|
Question : SQL Select Distinct Results (Complex)
|
|
I've got a problem that I can't seem to find a solution of. I thought the simple way would be to use SELECT DISTINCT, however, I think I need to use a JOIN of some sort, or perhaps a nested query, but no idea really how to build it. Let me give you a table example, and see if you can come up with a way to query it so I can get the data I need.
tblUsers: fldUser, fldTransaction, fldType
OK, I want a query that will return one user, a DISTINCT list of the transaction types, and the HIGHEST transaction for each type. i.e., if I'm querying user #1, and the table looks like this:
fldUser fldType fldTransaction 1 1 1 1 1 2 1 2 1 1 2 2 1 2 3 2 1 1 1 3 1
Then I want to see this as the result:
fldUser fldType fldTransaction 1 1 2 1 2 3 1 3 1
I've greatly simplified the idea of the tables I'll be using, but the list I want to return will all be in one table, will limit to one value on one field, needs to be DISTINCT for a second field, and for each record returned, needs to return the highest number of a third field.
Help?
"Z"
|
|
Answer : SQL Select Distinct Results (Complex)
|
|
SELECT fldUser, fldType, MAX(fldTransaction) FROM myTable GROUP BY fldUser, fldType
|
|
|
|