Viewing distinct records in a SQL query result

Tags:
SELECT statement
SQL Query
SQL Server Query
I have a set of records in a SQL query result. I only want to see one record, per the data field "ID". I am getting 2 records for some ID's, as there is one field that is different in the 2 records. This happens even if I start the query out with SELECT, DISTINCT, and then all of the fields I need. How can I just get the first record instead of two or more records per the field ID?

Answer Wiki

Thanks. We'll let you know when a new response is added.

This could be one way to do it:

<pre>SELECT id, min(description)
FROM YourTable
GROUP BY id;</pre>

Discuss This Question: 3  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • carlosdl
    The DISTINCT keyword will filter out those records in which all fields are exactly the same as some other record, that's why you are not getting the result you expected. You will need to define a criteria the choose what is the 'first' record, since the information will not neccesary come in the same order from the database every time you run your query, unless you specify some criteria to order by. This could be one way to do it:
    SELECT id, min(description)
    FROM YourTable
    GROUP BY id;
    69,510 pointsBadges:
    report
  • Kccrosser
    Carlosdl is correct on the problem and a suggested fix. Caution - pedantic rant follows - I am loathe to use "DISTINCT" in queries, unless I thoroughly understand why I am getting duplicate rows back. I have seen way too many queries that use DISTINCT to discard "excess" rows that are being caused by a poorly constructed query. DISTINCT is too often used to cover up a multitude of query problems, including Cartesian product queries, and I have (very recently) seen queries that appear to return a nicely formed set of 20 or so result records, but when analyzed further, turn out ot be generating an intermediate result of over 15,000 records due to a missing join condition, then discarding 14,980 of them as "dups" due to the DISTINCT keyword. And then users and DBAs wonder why their systems seem to be running slowly...
    3,830 pointsBadges:
    report
  • carlosdl
    You are absolutely correct Kccrosser. I have seen similar situations many times, most of them, due to incorrect join conditions. Thanks for complementing the answer.
    69,510 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following