SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 select query two sum fields based on diff conditio
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

cplusplus
Aged Yak Warrior

534 Posts

Posted - 07/23/2013 :  13:47:58  Show Profile  Reply with Quote
Is it possible to have two diff conditions for sum fields, I want to sum(RMQTY01) as IP,sum(RMQTY01) as OP FROM BPBSRMP

based on RMyear, RMDEPT, RMITEM.

this condition is specific to IP sum calc: AND RMPTYP <> 'I'

this condition is specific to OP sum calc: AND RMPTYP = 'I'

SELECT rmyear, rmdept,rmitem, sum(RMQTY01) as IP,sum(RMQTY01) as OP FROM BPBSRMP
WHERE RMHSP = 1
AND RMYEAR = 12
AND RMDEPT <> 0
AND RMRCTP in('D', 'T', 'O', '4')
AND RMUSE in('C', 'R');

Thank you very much for the helpful info.

Edited by - cplusplus on 07/23/2013 14:11:09

James K
Flowing Fount of Yak Knowledge

3578 Posts

Posted - 07/23/2013 :  13:54:42  Show Profile  Reply with Quote
quote:
Originally posted by cplusplus

I want to sum(RMQTY01) as IP,sum(RMQTY01) as OP FROM BPBSRMP

based on RMyear, RMDEPT, RMITEM.

this condition is specific to IP sum calc: AND RMPTYP <> 'I'

this condition is specific to OP sum calc: AND RMPTYP <> 'I'

SELECT rmyear, rmdept,rmitem, sum(RMQTY01) as IP,sum(RMQTY01) as OP FROM BPBSRMP
WHERE RMHSP = 1
AND RMYEAR = 12
AND RMDEPT <> 0
AND RMRCTP in('D', 'T', 'O', '4')
AND RMUSE in('C', 'R');

Thank you very much for the helpful info.

Since the condition is the same for both types of sums, and the quantity being summed is the same, wouldn't both sums produce the same result?

Regardless, what you want to do is use a case expression within the aggregate like shown below:
SELECT  rmyear ,
        rmdept ,
        rmitem ,
        SUM(CASE WHEN RMPTYP <> 'I' THEN RMQTY01 ELSE 0 END) AS IP ,
        SUM(CASE WHEN RMPTYP <> 'I' THEN RMQTY01 ELSE 0 END) AS OP
FROM    BPBSRMP
WHERE   RMHSP = 1
        AND RMYEAR = 12
        AND RMDEPT <> 0
        AND RMRCTP IN ( 'D', 'T', 'O', '4' )
        AND RMUSE IN ( 'C', 'R' );
Go to Top of Page

cplusplus
Aged Yak Warrior

534 Posts

Posted - 07/23/2013 :  14:12:02  Show Profile  Reply with Quote
Sorry James,

I didn't realize, that is a mistake by me. the condition is different, one <> & other =
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3578 Posts

Posted - 07/23/2013 :  14:29:31  Show Profile  Reply with Quote
I suspected something like that, but didn't want to assume that on your behalf ;)
Just change the <> to = in one of the case expressions.

If you can have NULL in the RMPTYP column, the corresponding RMQTY01 values will not get aggregated into either sum. If that is a concern, you should change the test to COALESCE(RMTYP,'X') <> 'I' (or the other way around depending on which bucket you want the rows that have RMTYP=NULL to fall into.
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 07/24/2013 :  02:00:20  Show Profile  Reply with Quote
quote:
Originally posted by James K

quote:
Originally posted by cplusplus

I want to sum(RMQTY01) as IP,sum(RMQTY01) as OP FROM BPBSRMP

based on RMyear, RMDEPT, RMITEM.

this condition is specific to IP sum calc: AND RMPTYP <> 'I'

this condition is specific to OP sum calc: AND RMPTYP <> 'I'

SELECT rmyear, rmdept,rmitem, sum(RMQTY01) as IP,sum(RMQTY01) as OP FROM BPBSRMP
WHERE RMHSP = 1
AND RMYEAR = 12
AND RMDEPT <> 0
AND RMRCTP in('D', 'T', 'O', '4')
AND RMUSE in('C', 'R');

Thank you very much for the helpful info.

Since the condition is the same for both types of sums, and the quantity being summed is the same, wouldn't both sums produce the same result?

Regardless, what you want to do is use a case expression within the aggregate like shown below:
SELECT  rmyear ,
        rmdept ,
        rmitem ,
        SUM(CASE WHEN RMPTYP <> 'I' THEN RMQTY01 ELSE 0 END) AS IP ,
        SUM(CASE WHEN RMPTYP <> 'I' THEN RMQTY01 ELSE 0 END) AS OP
FROM    BPBSRMP
WHERE   RMHSP = 1
        AND RMYEAR = 12
        AND RMDEPT <> 0
        AND RMRCTP IN ( 'D', 'T', 'O', '4' )
        AND RMUSE IN ( 'C', 'R' );
GROUP BY rmyear ,
        rmdept ,
        rmitem 




it needs a GROUP BY

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.14 seconds. Powered By: Snitz Forums 2000