Gossamer Forum
Home : General : Databases and SQL :

Help with SQL DISTINCT

Quote Reply
Help with SQL DISTINCT
Hi,

Is there any way to select several columns from a table, but only have the DISTINCT keyword apply to one of them. In other words, I would like to eliminate a row from my resulting dataset if only one of the columns is not unique.



thanks,

~Tyler
Quote Reply
Re: [festivus15] Help with SQL DISTINCT In reply to
I think you are confusing "rows" and "columns". DISTINCT is used to pull "distinct" values within cells of rows for a particular "column".

One suggestion, use a sub-query to identify the row that has a duplicated value in one of its columns.
========================================
Buh Bye!

Cheers,
Me
Quote Reply
Re: [Stealth] Help with SQL DISTINCT In reply to
I don't think i stated my question clearly. I am currently retrieving a table from a database that contains a bunch of columns. The only columns I "care" about are "StepNumber" and "DateCompleted". There can be multiple rows within the table with the same step number. They are made unique by the fact that they have different datecompleted fields. I would like to only display the most recent date for each step. I was trying to use the DISTINCT keyword to do this, although you might not actually be able to do that. Do you know how i would go about only selecting the most recent date for each individual step number?

thanks,

~tyler
Quote Reply
Re: [festivus15] Help with SQL DISTINCT In reply to
You can use:

SELECT StepNumber,DateCompleted FROM whatever ORDER BY DateCompleted DESC GROUP BY StepNumber

I think grouping slows stuff down a lot but thatd do what you want.
Quote Reply
Re: [clarkn] Help with SQL DISTINCT In reply to
i don't think this will work, b/c i want to display more than just those two columns and i don't want to simply order by date completed, i want to get rid of all but the most recent one.
Quote Reply
Re: [festivus15] Help with SQL DISTINCT In reply to
Again, using subqueries like the following:

Quote:

SELECT N.*
FROM tbl_TableName N
WHERE N.SomeColumn IN (SELECT ST.SomeColumn FROM tbl_SameTable ST WHERE (ST.SomeColumn = N.SomeColumn))


This should display a list of rows with duplicated values (non-unique) within the columns you specify...again using subqueries would probably solve your problem.

Note: If you are using MySQL version less than 3.22, then the above subquery would not work, but that is the only method I know that would solve your problem.
========================================
Buh Bye!

Cheers,
Me

Last edited by:

Stealth: Jul 9, 2002, 3:07 PM