Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

conditional summation 1

Status
Not open for further replies.

ethorn10

Programmer
Feb 18, 2003
406
US
Hey guys...this may seem trivial but I've been up way too long today and I just can't wrap my head around it. I need conditional summation performed based on a given value. For instance:

Code:
SELECT SUM(CASE WHEN tbl_a.aID = 1 
THEN SUM( crazy math goes here )
ELSE WHEN tbl_a.aID = 2 
THEN SUM( crazy math goes here )
ELSE 0 
END) AS 'total', MONTHNAME( tbl_a.field2 ) 
FROM tbl_a

It doesn't seem to like the sum of the case statement stuff. Says an error in my syntax near ') AS 'total'. This is probably very true..I just don't see it. Sorry to bother...thanks in advance.
 
Code:
SELECT SUM(
        CASE WHEN tbl_a.aID = 1 
             THEN ( crazy math goes here )
             WHEN tbl_a.aID = 2 
             THEN ( crazy math goes here )
             ELSE 0 
         END) AS total
     , MONTHNAME( tbl_a.field2 ) 
  FROM tbl_a
group
    by MONTHNAME( tbl_a.field2 )

r937.com | rudy.ca
 
Rudy...thanks man. I knew I could count on you to help me out. When I woke up from a much needed nap, I tried your response but it still wasn't quite there. Turns out in the 'crazy math goes here' I was using another sum function and man did it not like that. Kept crying invalid use of the group function so I removed it and got expected results.

Still a star for making me see straight.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top