Sql sum if

zovche

Board Regular
Joined
Mar 21, 2013
Messages
125
Using SQL, is it possible to group records and sum quantities only if date is higher then specific date?

For example my table has records:
ITEMORDERQTYDATEDEADLINE
I001P001501.12.201505.12.2015
I001P001310.12.201505.12.2015
I001P001720.12.201505.12.2015

<tbody>
</tbody>


I would like to group them and get total and "Sum QTY where DATE >DEADLINE" to get
ITEMORDERTotal QTYQTY after DEADLINE
I001P0011510

<tbody>
</tbody>


Don't worry about grouping them, I already did that, just not sure if it's possible to add part in Select and somehow define column "QTY after DEADLINE".

Thank you
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Just add a calculated IIF statement that checks to see if Date > Deadline, and if it is, return the QTY, else return 0.
Then apply the SUM function to this calculated field and it should return what you want.
 
Upvote 0
What flavour of SQL are you using/database?

Here's MSSQL
Rich (BB code):
SELECT Item
	,[order]
	,sum(qty) Total_Qty
	,sum(CASE 
			WHEN [Date] > Deadline
				THEN qty
			ELSE 0
			END) Qty_after_deadline
FROM orders
GROUP BY item
	,[order]
 
Last edited:
Upvote 0
I'm using DB2 database, not sure what flavour it is, but I will try both of your suggestions, hopefully one will work. Thank you kindly for help so far
 
Upvote 0
What flavour of SQL are you using/database?

Here's MSSQL
Rich (BB code):
SELECT Item
    ,[order]
    ,sum(qty) Total_Qty
    ,sum(CASE 
            WHEN [Date] > Deadline
                THEN qty
            ELSE 0
            END) Qty_after_deadline
FROM orders
GROUP BY item
    ,[order]

Just to let you know that I managed to make it,

sum(CASE WHEN [Date] > Deadline THEN QTY ELSE 0 END) AS "Qty_after_deadline"

Thank you a lot, have a great day
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,550
Members
449,088
Latest member
davidcom

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top