locked
DISTINCT doesn't seem to be working RRS feed

  • Question

  • User1580573345 posted

    Hi all,

     I am using SQLEXPRESS 2005 and can't figure out why my SQL query is not working.

    Select DISTINCT ads.clientid, ads.ad, ads.adid, ads.cost, ads.description, ads.bold, ads.info, 
    ads.contact, ads.coupon, ads.logo, ads.map, ads.webpage, ads.weburl, tclients.clientname, 
    tclients.clientphone, tclients.addNumber, tclients.addStreet, tclients.addsuite, tclients.addcity, 
    tclients.addstate, tclients.addzip from tclients, ads where tclients.clientid=ads.clientid 
    AND  AdActive=1 ORDER BY COST DESC

     I expect only one row to be returned for each ads.clientid.  However, every single record that matches the query is being returned, which results in multiple records with the same clientid.

    This happens if it is ads.clientid or tclients.clientid.

     Martin

    Thursday, September 28, 2006 4:41 PM

Answers

  • User1580573345 posted

    Motley,

    This SEEMS to work, but I'm not a SQL expert, so I can't be sure it will in real data.  Does it look good to you?  The bolded part is what I added.

     

    Select DISTINCT ads.clientid, ads.ad, ads.adid, ads.cost, ads.description, ads.bold, ads.info, 
    ads.contact, ads.coupon, ads.logo, ads.map, ads.webpage, ads.weburl, tclients.clientname, 
    tclients.clientphone, tclients.addNumber, tclients.addStreet, tclients.addsuite, tclients.addcity, 
    tclients.addstate, tclients.addzip from tclients, ads where tclients.clientid=ads.clientid 
    AND  AdActive=1 
    AND cost in (select max(ads.cost) from ads group by clientid)
    ORDER BY COST DESC
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 28, 2006 8:19 PM

All replies

  • User153155060 posted
    DISTINCT returns unique rows based on all columns in the query, not just the first column (clientid) that you've specified.
    Thursday, September 28, 2006 6:07 PM
  • User1580573345 posted

    Ah - I'm glad it it working as intended and I am not going mad...

    How would I go about getting what I wanted then?  Just one clientid.

    Thursday, September 28, 2006 6:10 PM
  • User-1225738063 posted

    Can't answer that.

    You said what you wanted.

    You showed the query you have and the columns that you want.

    The two don't mix, or the distinct would have worked anyhow.  You obviously have multiple records with the same clientid, and differing data.  WHICH of the differing data would you like when a clientid has multiple?

    Assuming your fingers have names (index,middle,ring,pinky,thumb), and your hands have names (left, right).

    Please tell me the length of your finger on each hand.  And I only want one 2 entries, one for each hand.  And if you are left scratching your head, it's because the question doesn't match the data, and you can't answer it.  If I said, tell me the length of the longest finger on each hand, NOW you can answer, or I could say tell me the length of your index finger on each hand, etc.

    Thursday, September 28, 2006 6:54 PM
  • User1580573345 posted

    Thanks for that post - it helped me realize what I am doing wrong in my query.

     I assumed because I was ordering by COST that it would take the first record for each clientid.  In your example, it would order all my fingers by length, then take the longest one from each hand.

     Since I know this is not the case, I know have to figure out how to re-write the query.

     

    Thursday, September 28, 2006 8:11 PM
  • User1580573345 posted

    Motley,

    This SEEMS to work, but I'm not a SQL expert, so I can't be sure it will in real data.  Does it look good to you?  The bolded part is what I added.

     

    Select DISTINCT ads.clientid, ads.ad, ads.adid, ads.cost, ads.description, ads.bold, ads.info, 
    ads.contact, ads.coupon, ads.logo, ads.map, ads.webpage, ads.weburl, tclients.clientname, 
    tclients.clientphone, tclients.addNumber, tclients.addStreet, tclients.addsuite, tclients.addcity, 
    tclients.addstate, tclients.addzip from tclients, ads where tclients.clientid=ads.clientid 
    AND  AdActive=1 
    AND cost in (select max(ads.cost) from ads group by clientid)
    ORDER BY COST DESC
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, September 28, 2006 8:19 PM