October 8, 2013 at 3:04 am
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.
October 8, 2013 at 4:19 am
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;-)
October 8, 2013 at 4:22 am
Bhuvnesh (10/8/2013)
select * from TABLE where countryid = @countryselect * 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/
October 8, 2013 at 5:26 am
Bhuvnesh (10/8/2013)
select * from TABLE where countryid = @countryselect * 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
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
October 8, 2013 at 8:19 am
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;-)
October 8, 2013 at 8:29 am
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
Change is inevitable... Change for the better is not.
October 8, 2013 at 8:39 am
October 9, 2013 at 12:21 am
Thank you all of you for your immediate reply.
October 9, 2013 at 2:09 am
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;-)
October 9, 2013 at 2:36 am
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
October 9, 2013 at 2:52 am
Jeff Moden (10/8/2013)
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.
Can you please explain how the OR is an implied AND. I don't see OR & AND returning the same records.
CREATE TABLE MyTable
(UserID INT,
CityID INT,
StateID INT,
CountryID INT);
-- Two users, in the same state and country but different cities
INSERT INTO MyTable VALUES (1, 1, 1, 1);
INSERT INTO MyTable VALUES (2, 2, 1, 1);
SELECT
*
FROM
MyTable
WHERE
CityID = 1
OR StateID = 1
OR CountryID = 1;
SELECT
*
FROM
MyTable
WHERE
CityID = 1
AND StateID = 1
AND CountryID = 1;
DROP TABLE MyTable;
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply