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 8, 2013 3:04 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 9, 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 8, 2013 4:19 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 5:03 AM
Points: 2,840, Visits: 3,975
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 8, 2013 4:22 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 7:24 AM
Points: 1,952, Visits: 2,396
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 8, 2013 5:26 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 6:52 AM
Points: 6,872, Visits: 14,185
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 8, 2013 8:19 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 5:03 AM
Points: 2,840, Visits: 3,975
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 8, 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 @ 8:51 PM
Points: 35,606, Visits: 32,190
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."

(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 8, 2013 8:39 AM


Hall of Fame

Hall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of FameHall of Fame

Group: General Forum Members
Last Login: Today @ 10:08 AM
Points: 3,937, Visits: 8,927
What about normalizing data?


Luis C.
Are you seriously taking the advice and code from someone from the internet without testing it? Do you at least understand it? Or can it easily kill your server?

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

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

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


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Wednesday, November 19, 2014 5:03 AM
Points: 2,840, Visits: 3,975
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 9, 2013 2:36 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Today @ 10:51 AM
Points: 5,438, Visits: 10,140
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