I am trying to group some quantities together but I keep getting stuck on the last stage....
Which comes out like this
SM_STOCK_CODE sm_type smyear Bought Used Sold
EFL052 A 2004 0 -76 0
EFL052 A 2005 0 431 0
EFL052 A 2006 0 -55 0
EFL052 L 2004 0 0 0
EFL052 P 2004 76 0 0
EFL052 P 2005 761 0 0
EFL052 S 2005 0 0 679
And I want it to come out like this
SM_STOCK_CODE smyear Bought Used Sold
EFL052 2004 76 -76 0
EFL052 2005 761 431 679
EFL052 2006 0 -55 0
but I keep getting an aggregate error when I change the sql....
Ideas ?
Code:
SELECT SM_STOCK_CODE, sm_type, YEAR(SM_DATE)AS smyear,
CASE WHEN sm_type = 'P' THEN SUM(CASE WHEN sm_status = 'O' THEN 0 - sm_quantity ELSE sm_quantity END)ELSE 0 END AS Bought,
CASE WHEN sm_type = 'A' THEN SUM(CASE WHEN sm_status = 'O' THEN 0 - sm_quantity ELSE sm_quantity END)ELSE 0 END AS Used,
CASE WHEN sm_type = 'S' THEN SUM(CASE WHEN sm_status = 'O' THEN 0 - sm_quantity ELSE sm_quantity END) ELSE 0 END AS Sold
FROM STK_MOVEMENTS
GROUP BY YEAR(SM_DATE), SM_STOCK_CODE, sm_type
HAVING (SM_STOCK_CODE = 'efl052')
Which comes out like this
SM_STOCK_CODE sm_type smyear Bought Used Sold
EFL052 A 2004 0 -76 0
EFL052 A 2005 0 431 0
EFL052 A 2006 0 -55 0
EFL052 L 2004 0 0 0
EFL052 P 2004 76 0 0
EFL052 P 2005 761 0 0
EFL052 S 2005 0 0 679
And I want it to come out like this
SM_STOCK_CODE smyear Bought Used Sold
EFL052 2004 76 -76 0
EFL052 2005 761 431 679
EFL052 2006 0 -55 0
but I keep getting an aggregate error when I change the sql....
Ideas ?