Viewing distinct records in a SQL query result
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?

Software/Hardware used:
ASKED: April 3, 2009  5:48 PM
UPDATED: April 7, 2009  11:30 PM

Answer Wiki:
This could be one way to do it: <pre>SELECT id, min(description) FROM YourTable GROUP BY id;</pre>
Last Wiki Answer Submitted:  April 3, 2009  6:03 pm  by  carlosdl   63,535 pts.
All Answer Wiki Contributors:  carlosdl   63,535 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _


 

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;
 63,535 pts.

 

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

 

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.

 63,535 pts.