When grouping is there a simple way to get the first or top value out of a column. In the following example I want the last value in [Text] if sorted by [Date] descending.
SELECT
[Id],
MAX([Date]),
TOP 1 [Text], <-- How do I do do this
COUNT(*)
FROM
[ExampleTable]
GROUP BY [Id]
ORDER BY [Date] DESC
Thanks in advance.
If you want the text corresponding to that [Id] & that [MaxDate], you can do this:
if [Id] & [Date] form a primary key you will have a single row per (Id, Date) other wise many. If you really want one, either you put other constraints or use MAX or MIN::
If you want just a value for Text you can use MAX or MIN: