Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

Filter Countries, States and Cities which in same row in one table. Expand / Collapse
Author
Message
Posted Tuesday, October 08, 2013 3:04 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 09, 2013 12:13 AM
Points: 2, Visits: 6
I have table which contains the data of the Users with different Country, State and city, in which State and city may be null or may not be null. And all these data are in one table.

Table is like
UserId Not Null,
Username Not Null,
Fullname Not Null,
CountryId Not Null,
StateId Null,
CityId Null


I need data based on
1. CountryId
2. CountryId and StateId,
3. CountryId, StateId and CityId

Please suggest me the possible queries.
Post #1502503
Posted Tuesday, October 08, 2013 4:19 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, March 14, 2014 2:19 AM
Points: 2,820, Visits: 3,916
select * from TABLE where countryid = @country
select * from TABLE where countryid = @country OR stateID = @stateid
select * from TABLE where countryid = @country OR stateID = @stateid OR Cityid = @Cityid


Here @country ,stateID and @Cityid are parameters.


-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1502532
Posted Tuesday, October 08, 2013 4:22 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Monday, April 14, 2014 11:50 PM
Points: 1,868, Visits: 2,275
Bhuvnesh (10/8/2013)
select * from TABLE where countryid = @country
select * from TABLE where countryid = @country OR stateID = @stateid
select * from TABLE where countryid = @country OR stateID = @stateid OR Cityid = @Cityid


Here @country ,stateID and @Cityid are parameters.

+1



_______________________________________________________________
To get quick answer follow this link:
http://www.sqlservercentral.com/articles/Best+Practices/61537/
Post #1502533
Posted Tuesday, October 08, 2013 5:26 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, April 17, 2014 9:33 AM
Points: 6,754, Visits: 12,854
Bhuvnesh (10/8/2013)
select * from TABLE where countryid = @country
select * from TABLE where countryid = @country OR stateID = @stateid
select * from TABLE where countryid = @country OR stateID = @stateid OR Cityid = @Cityid


Here @country ,stateID and @Cityid are parameters.


SELECT * FROM TABLE WHERE countryid = @country
SELECT * FROM TABLE WHERE countryid = @country AND stateID = @stateid
SELECT * FROM TABLE WHERE countryid = @country AND stateID = @stateid AND Cityid = @Cityid




“Write the query the simplest way. If through testing it becomes clear that the performance is inadequate, consider alternative query forms.” - Gail Shaw

For fast, accurate and documented assistance in answering your questions, please read this article.
Understanding and using APPLY, (I) and (II) Paul White
Hidden RBAR: Triangular Joins / The "Numbers" or "Tally" Table: What it is and how it replaces a loop Jeff Moden
Exploring Recursive CTEs by Example Dwain Camps
Post #1502549
Posted Tuesday, October 08, 2013 8:19 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, March 14, 2014 2:19 AM
Points: 2,820, Visits: 3,916
ChrisM@Work (10/8/2013)
SELECT * FROM TABLE WHERE countryid = @country
SELECT * FROM TABLE WHERE countryid = @country AND stateID = @stateid
SELECT * FROM TABLE WHERE countryid = @country AND stateID = @stateid AND Cityid = @Cityid



totally agree with you Chris.. but i think here OR operator will be more reliable because with correct data
OR and AND will return same records ..What do you think ?


-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1502641
Posted Tuesday, October 08, 2013 8:29 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: General Forum Members
Last Login: Yesterday @ 10:06 PM
Points: 35,978, Visits: 30,269
Bhuvnesh (10/8/2013)
ChrisM@Work (10/8/2013)
SELECT * FROM TABLE WHERE countryid = @country
SELECT * FROM TABLE WHERE countryid = @country AND stateID = @stateid
SELECT * FROM TABLE WHERE countryid = @country AND stateID = @stateid AND Cityid = @Cityid



totally agree with you Chris.. but i think here OR operator will be more reliable because with correct data
OR and AND will return same records ..What do you think ?


Gosh, I wouldn't rely on that nuance especially for the sake of clarity. I would absolutely use AND in this case just to avoid any confusion in the heat of an emergency fix, should it ever occur. A newbie wouldn't understand the ORs are an implied AND simply because of the IDs involved.


--Jeff Moden
"RBAR is pronounced "ree-bar" and is a "Modenism" for "Row-By-Agonizing-Row".

First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column."

"Change is inevitable. Change for the better is not." -- 04 August 2013
(play on words) "Just because you CAN do something in T-SQL, doesn't mean you SHOULDN'T." --22 Aug 2013

Helpful Links:
How to post code problems
How to post performance problems
Post #1502647
Posted Tuesday, October 08, 2013 8:39 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Yesterday @ 9:16 PM
Points: 2,763, Visits: 5,915
What about normalizing data?


Luis C.
I am a great believer in luck, and I find the harder I work the more I have of it. Stephen Leacock

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1502652
Posted Wednesday, October 09, 2013 12:21 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 09, 2013 12:13 AM
Points: 2, Visits: 6
Thank you all of you for your immediate reply.
Post #1502925
Posted Wednesday, October 09, 2013 2:09 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Friday, March 14, 2014 2:19 AM
Points: 2,820, Visits: 3,916
Jeff Moden (10/8/2013)
Gosh, I wouldn't rely on that nuance especially for the sake of clarity.

Thanks Jeff for keeping close eye on all the replies , it actually helps me a lot .. you know your quoted word "GOSH" made me to read your and mine first reply 6 times to see what mistake i made and then finally realised that
i overlooked the below OP's text.

I need data based on
1. CountryId
2. CountryId and StateId,
3. CountryId, StateId and CityId




-------Bhuvnesh----------
I work only to learn Sql Server...though my company pays me for getting their stuff done
Post #1502957
Posted Wednesday, October 09, 2013 2:36 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, April 17, 2014 8:58 AM
Points: 5,077, Visits: 8,918
Luis Cazares (10/8/2013)
What about normalizing data?

Absolutely. If you have any control over the structure of the table, you should remove the CountryId and StateId columns, since they are attributes of the city, not of the user.

John
Post #1502964
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse