Tuesday, 13 November 2018

Retrieving the last record in each group - SQL Server.

sql query to get last record of each group in every month

Description:

In this example we explain that how to get Last Record of each group using SQL Server. Alternatively, SQL query to retrieve Last record of each product or group using SQL Server. Alternatively, how to get last record of each group in every month of specific range using SQL query. Alternatively, how do I get latest record of each group using SQL Server query. Alternatively, how to select Last record of each group using SQL Server.

So below is the SQL query that will help you to select or retrieve last record of each group in each month.
Query:

WITH ranked_messages AS (
  SELECT m.*, ROW_NUMBER() OVER (PARTITION BY name, datepart(MM, ModifiedDate) order by ModifiedDate desc) AS rn
  FROM messages AS m
)
SELECT * FROM ranked_messages WHERE rn = 1;



This entry was posted in :

0 comments:

Post a Comment