Filter Countries, States and Cities which in same row in one table.

  • 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.

  • 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;-)

  • 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/

  • 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

  • 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;-)

  • 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.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • What about normalizing data?

    Luis C.
    General Disclaimer:
    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?

    How to post data/code on a forum to get the best help: Option 1 / Option 2
  • Thank you all of you for your immediate reply.

  • 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;-)

  • 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

  • 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;

    The SQL Guy @ blogspot[/url]

    @SeanPearceSQL

    About Me[/url]

Viewing 11 posts - 1 through 10 (of 10 total)

You must be logged in to reply to this topic. Login to reply