Skip to content
Advertisement

Query to sum the top “n” records

I would like to sum the forecast qty column for only the first “n” date records. For example for each item number the earliest(or first) three date records

Current Query:

Table

enter image description here

Desired Result

enter image description here

Advertisement

Answer

Assign a row number to each record, partitioning by Item (this will start a new row counter for each Item) and ordering by DemandDate. Then sum the records where the row number is <= N, grouping by Item:

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