Let's say:
I want to query colA, colB and colC in my table.
I want to see DISTINCT values but I don't want colA to be a criteria for distinction.
Omitting colA isn't an option.
What's the best way to structure that query?
Let's say:
I want to query colA, colB and colC in my table.
I want to see DISTINCT values but I don't want colA to be a criteria for distinction.
Omitting colA isn't an option.
What's the best way to structure that query?
Perhaps this site will help? It has some detailed instructions and it appears to be what you're trying to do.
http://oscarvalles.wordpress.com/2008/01/28/sql-distinct-on-one-column-only/
I dont know the syntax for a temporary table, so pseudocode if you please =)
Select Distinct ColB, ColC into @Temp
From SomeTable
Where (predicates)
Select ColA, ColB, ColC
From SomeTable
Inner Join @Temp on (SomeTable.ColB = @Temp.ColB and SomeTable.ColC = @Temp.ColC)
Where (predicates) /* Added for comments */
Hope this helps.
There are two cases here. Let's say you have the data
A B C (columns)
a b c1
a b c2
Taking distinct values of A, B gives just one result (a,b), with two values for column C. So the question is do you want to see all values of C or just one value for each distinct value of columns A and B?
If you want to see just one value of C, then you can write
SELECT A, B, MAX(C) FROM YourTable
GROUP BY A, B
On the other hand, if you want to see all values for C then
SELECT DISTINCT A, B, C FROM YourTable WHERE ROW(A,B) IN
(SELECT A, B FROM YourTable
GROUP BY A, B)
gives you that. This last alternative is needed if there are other columns in the table.