Skip to content
Advertisement

Sum last two records including last record of a group

In SQL Server 2017, how do I sum the last two records and show the last record in a single query?

Expected output:

I tried using SUM with LAST_VALUE with ORDER BY

Advertisement

Answer

You can filter out rows by using the ROW_NUMBER() window function, as in:

See SQL Fiddle.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement