Comments on: Viewing distinct records in a SQL query result Sat, 27 Dec 2014 09:23:37 +0000 hourly 1 By: carlosdl Tue, 07 Apr 2009 23:30:14 +0000 #comment-61993 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.

By: kccrosser Mon, 06 Apr 2009 20:45:01 +0000 #comment-61931 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…

By: carlosdl Fri, 03 Apr 2009 18:03:04 +0000 #comment-61860 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