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!

CASE statement with SUM 1

Status
Not open for further replies.

ethorn10

Programmer
Feb 18, 2003
406
US
Hello all...

Quite some time ago, I got a query straightened out here by some helpful MVPs. My situation has changed slightly. Previously, I had a query that was doing a SUM for each month. Now I'd like to add to that given a condition but I'm stuck in one mode of thinking. It isn't adding up right and I can tell why, I just can't fix it. What I have so far:
Code:
SELECT 
CASE WHEN a.extra_id = 1 THEN (format(sum(b.price+25),0))
ELSE (format(sum(b.price),0)) END AS total,
date_format(a.date_applied, '%M' ) AS date_applied 
FROM tbl_details a inner join
	tbl_sessions b on a.id = b.id
WHERE a.paid=1 
	AND b.sessiontitle = "demo" 
GROUP BY date_format(a.date_applied, '%m' )
ORDER BY MONTH(a.date_applied)

I realize why I'm getting incorrect totals (the CASE statement) but, like I said, I can't get outside of this box I'm in. Help is greatly appreciated.
 
It looks like the SUM should be moved outside the CASE:
[tt]
SELECT
FORMAT(
SUM(
CASE
WHEN a.extra_id = 1 THEN (b.price+25)
ELSE (b.price)
END
),
0
)
AS total,
date_format(a.date_applied, '%M' ) AS date_applied
FROM
tbl_details a
inner join tbl_sessions b on a.id = b.id
WHERE a.paid=1 AND b.sessiontitle = "demo"
GROUP BY date_format(a.date_applied, '%m' )
ORDER BY MONTH(a.date_applied)
[/tt]
 
Augh!! Nice work Tony! I had a feeling it wasn't much, just got stuck in that wrong mindset. A star for you.

Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top