Subject | [firebird-support] Re: order by with grouping? |
---|---|
Author | Svein Erling Tysvær |
Post date | 2013-01-25T07:02:35Z |
>i might be phrasing it weird butphrased another way:
>order by tag ( where date is most recent)
>group by ID
>
>i want the IDs together or groupedYou're not phrasing it weird, Kelly, it is a slightly weird requirement! But here goes (it won't work on Firebird 1.5)
>
>i want the IDs sorted by the Tag of the most recent one.
with MyCTE as
(SELECT ID, Tag
FROM KellyTbl K1
WHERE NOT EXISTS(SELECT * FROM KellyTbl K2
WHERE K1.ID = K2.ID
AND K1.Date < K2.Date))
SELECT KT.Tag, KT.ID, KT.Date
FROM KellyTbl KT
JOIN MyCTE M ON M.ID = KT.ID
ORDER BY M.Tag, KT.Date
This will at least almost give you what you specified as desired output in your first email, though ID 22 will be at the bottom since the record of ID 22 with the latest date is HHH and not AAA.
HTH,
Set