I want to select the highest transaction amount and the corresponding Album Name to that amount.
This is what I have so far:
SELECT a.AlbumName, MAX(t.TransAmt) AS HighestSale
FROM TRANSACTIONS t
JOIN COLLECTIONS c ON c.CollectionID = t.CollectionID
JOIN ALBUMS a ON a.AlbumID = c.AlbumID
GROUP BY a.AlbumName
I know I need a HAVING in there but I am unsure how to implement it.
This is what I get currently:
| AlbumName | HighestSale |
|---|---|
| Cassadaga | 10.00 |
| I Walk The Line | 13.00 |
| Sweet Revenge | 14.00 |
| The Tree of Forgiveness | 12.00 |
I only want to see the HighestSale (14.00) and then the AlbumName (Sweet Revenge)
You can do this using
ORDER BYand fetching one row. In Standard SQL"Some databases don't support the standard
FETCHclause, so you might wantLIMITorSELECT TOP (1)or something else.