Bubble Bub
Bubble Bub

Reputation: 701

Select Max() and Min() date and Sum up quantity

I have table with data as below

StartDate   EndDate      ID        Qty
2017-09-01 2017-10-01 PJFC1000205   1
2017-10-01 2017-11-01 PJFC1000205   2
2017-11-01 2017-12-01 PJFC1000205   3
2017-12-01 2018-01-01 PJFC1000205   4

I wish sum up the qty and wish the output as below

StartDate      EndDate      ID           Qty
2017-09-01    2018-01-01   PJFC1000205    10

Is it possible to do so? How I can do it?
I'm using MSSQL 2014. Please guide me, thanks.

Upvotes: 1

Views: 54

Answers (1)

emendez
emendez

Reputation: 440

Using the group by on ID and aggregate functions on the other fields should do the trick.

 SELECT min(Start_Date), max(End_Date), ID, sum(Qty)
    FROM table
    GROUP BY ID;

Upvotes: 1

Related Questions