0

I have an SQL table with a column categoryX that contains dates (format: yyyy-mm-dd). How can i group the table by categories in this column and get the newest (most recent) date for each group ?

I am looking for something similar to the below which only counts the categories for each group - just instead of the count I would like to get the newest date for each group.

Here are the details on the table and column:

ALTER PROCEDURE [dbo].[CountRequests]
AS
BEGIN
    SET NOCOUNT ON;
    SELECT      categoryX,
                COUNT(*) AS categoryCount
    FROM        LogRequests
    WHERE       logStatus = 'active'
    AND         statusSOP != 'Published'
    GROUP BY    categoryX
    ORDER BY    categoryCount desc, categoryX
    FOR XML PATH('categoryX'), ELEMENTS, TYPE, ROOT('ranks')
END

Many thanks for any help with this

user2571510
  • 11,167
  • 39
  • 92
  • 138

1 Answers1

3

Your query will be something similar to

SELECT      categoryX,
            MAX(CategoryDate) AS LatestDate
FROM        YourTable
GROUP BY    categoryX
Szymon
  • 42,577
  • 16
  • 96
  • 114