the TSQL code for this query...is needed.

  • A SELECT statement that returns a name,city and state of each vendor that is located in a unique city and state. In other words, don't include vendors that have a city and state that is common with another vendor.

  • Please post table definitions, sample data and desired output. Read this to see the best way to post this to get quick responses.

    http://www.sqlservercentral.com/articles/Best+Practices/61537/

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • As Gail said, please give us more info. Anything provided without that info us just a guess.

    Jason...AKA CirqueDeSQLeil
    _______________________________________________
    I have given a name to my pain...MCM SQL Server, MVP
    SQL RNNR
    Posting Performance Based Questions - Gail Shaw[/url]
    Learn Extended Events

  • Hi...seems that from your previous posts that you are currently a student.

    ...no probs with that, but you will learn a lot faster if you at least provide what you you have tried so far.

    as requested before ...learn to provide the required table definitions, sample data and desired output...if this is unclear then ask for help.

    to answer your question....

    heres an idea to start you off

    SELECT city, COUNT(*) AS SingleVendor_byCity

    FROM YOURDATA_TABLE

    GROUP BY city

    HAVING (COUNT(*) = 1)

    ...i am assuming that a city can only be associated with one state

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • Yes I am a new student to TSQL and SS2K8. its a bit different than DB2s SQL. A lot of new code to learn. Last night at class I had the instructor explain it to me and the class. The solution is this:

    SELECT VendorName

    ,VendorCity

    ,VendorState

    FROM Vendors

    WHERE VendorCity NOT IN

    (SELECT v1.VendorCity

    FROM Vendors AS v1

    JOIN Vendors AS v2

    ON (v1.VendorID <> v2.VendorID) AND

    (v1.VendorCity = v2.VendorCity AND

    v1.VendorState = v2.VendorState));

  • Ask your "instructor" what will happen if two different states have the city with the same name?

    The solution given to you is not only wrong, but also would perform badly as well.

    One of the way to achieve it would be:

    SELECT v1.VendorName, v1.VendorCity, v1.VendorState

    FROM Vendor AS v1

    LEFT JOIN Vendor AS v2

    ON v2.VendorCity = v1.VendorCity

    AND v2.VendorState = v1.VendorState

    AND v2.VendorId != v1.VendorId

    WHERE v2.VendorId IS NULL

    OR

    SELECT v1.VendorName, v1.VendorCity, v1.VendorState

    FROM Vendor AS v1

    WHERE NOT EXISTS( SELECT 1

    FROM Vendor AS v2

    WHERE v2.VendorCity = v1.VendorCity

    AND v2.VendorState = v1.VendorState

    AND v2.VendorId != v1.VendorId )

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • J Livingston SQL (2/8/2012)


    Hi...seems that from your previous posts that you are currently a student.

    ...no probs with that, but you will learn a lot faster if you at least provide what you you have tried so far.

    as requested before ...learn to provide the required table definitions, sample data and desired output...if this is unclear then ask for help.

    to answer your question....

    heres an idea to start you off

    SELECT city, COUNT(*) AS SingleVendor_byCity

    FROM YOURDATA_TABLE

    GROUP BY city

    HAVING (COUNT(*) = 1)

    ...i am assuming that a city can only be associated with one state

    I believe the COUNT(*) needs to be COUNT(DISTINCT VendorName) to allow a single vendor to have more than one presence in the city.

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

  • Eugene and wurkenmomz ,

    The <> in both of your code creates a "double Triangular Join". If you use that code (making the appropriate substitutions for the different table) on the AdventureWorks.Person.Address table, it creates more than 2 million internal rows to filter through just 19 thousand and takes more than 6 seconds to run on my 10 year old box.

    Please see the following article for what "Triangular Joins" are and how they can devestate a server by generating internal rows.

    http://www.sqlservercentral.com/articles/T-SQL/61539/

    I could be wrong because the OP didn't provide any test data nor expected results for that data, but I believe the following is what is needed and it doesn't have the "Triangular Join" problem.

    SELECT addr1.VendorName, addr1.VendorCity, addr1.VendorState

    FROM dbo.Vendors addr1

    INNER JOIN

    (

    SELECT VendorCity, VendorState

    FROM dbo.Vendors

    GROUP BY VendorCity, VendorState

    HAVING COUNT(DISTINCT VendorName) = 1

    ) unqaddr

    ON addr1.VendorCity = unqaddr.VendorCity

    AND addr1.VendorState = unqaddr.VendorState

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

  • Jeff, that why I said "one of". I was afraid to recommend using "HAVING COUNT..." as it could be even harder to explain to his "instructor".

    However, if you look into instructor provided solution, you can see that Vendor table has VendorId, therfore I wouldn't recommend "distincting" by name, as you may have two different John Smith's easely and happily leaving in the same city, therefore records for this city shouldn't be returned as per requirements. You are absolutely right about trangular joins. But, I still think that the query should not check for distinct names, instead HAVING COUNT(*) = 1 should be used.

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • I believe the COUNT(*) needs to be COUNT(DISTINCT VendorName) to allow a single vendor to have more than one presence in the city.

    absolutely agree...but should it be VendorID (not vendorName) ??

    .....(.but in defence I did say "an idea to start you off" and in the absence of any data ???.....:-) )

    ________________________________________________________________
    you can lead a user to data....but you cannot make them think
    and remember....every day is a school day

  • J Livingston SQL (2/9/2012)


    I believe the COUNT(*) needs to be COUNT(DISTINCT VendorName) to allow a single vendor to have more than one presence in the city.

    absolutely agree...but should it be VendorID (not vendorName) ??

    .....(.but in defence I did say "an idea to start you off" and in the absence of any data ???.....:-) )

    If the VendorID is ID in the Vendor table, it is right to assume it is unique. Therefore, there is no need to do COUNT(DISTINCT...)

    COUNT(*) will be just fine and right.

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

  • Eugene Elutin (2/9/2012)


    J Livingston SQL (2/9/2012)


    I believe the COUNT(*) needs to be COUNT(DISTINCT VendorName) to allow a single vendor to have more than one presence in the city.

    absolutely agree...but should it be VendorID (not vendorName) ??

    .....(.but in defence I did say "an idea to start you off" and in the absence of any data ???.....:-) )

    If the VendorID is ID in the Vendor table, it is right to assume it is unique. Therefore, there is no need to do COUNT(DISTINCT...)

    COUNT(*) will be just fine and right.

    "It Depends". If there are two of the same franchise (with the same VendorID) in the same city, the COUNT(*) won't work correctly.

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

  • Thank you everyone for your help the answers, my instructor helped me figure this one out. The answer he was looking for was this:

    SELECT VendorName

    ,VendorCity

    ,VendorState

    FROM Vendors

    WHERE VendorCity NOT IN

    (SELECT v1.VendorCity

    FROM Vendors AS v1

    JOIN Vendors AS v2

    ON (v1.VendorID <> v2.VendorID) AND

    (v1.VendorCity = v2.VendorCity AND

    v1.VendorState = v2.VendorState));

    It works!!! Great!!!

  • Also, he states that using the Distinct will not get the recommended results and can be very unreliable, in these cases. He does this for a living.

  • wurkenmomz (2/10/2012)


    Also, he states that using the Distinct will not get the recommended results and can be very unreliable, in these cases. He does this for a living.

    Perfect. The instructor must be correct especially since he does this for a living. The rest of us all do this in our spare time just for fun and only every other weekend and only if it's not nice outside. 😛

    Just remember that your instructor is the one that wrote the "Triangular Join" and then lookup "DISTINCT" in Books Online and see if it's really as "unreliable" as he says. 😉

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

Viewing 15 posts - 1 through 15 (of 16 total)

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