That's also right:
select sum( case 1 when score=7 then score * 2 else score end) from "foo"; [EMAIL PROTECTED] on 03/07/2003 11:28:03 AM To: Peter D Bethke <[EMAIL PROTECTED]>, MySQL List <[EMAIL PROTECTED]> cc: (bcc: David P. Giragosian/MDACC) Subject: Re: CASE statement within SUM() At 11:29 -0500 3/7/03, Peter D Bethke wrote: >Hello listers, > >In mysql, is it possible imbed a conditional CASE statement within a SUM() >function to effect how the SUM() returns the final value for a set of rows? > >For example, if I have a table "foo" with column "score", and "score" has >the rows with the values 1 to 10, can I write the SUM() in such a way to >double the value of the number 7 for example to the final sum? Ie when the >SUM() function encounters the number 7, add (score*2) instead of just score >to the final total. > >Something like: > >SUM(CASE WHEN score = 7 THEN score*2 ELSE score END) AS final_sum > >(Of course this doesn't seem to work, but it's the best I can approximate) There's no reason it shouldn't work. What was the exact query you used, and what was the result? What error message, for example. > >Any help, much much appreciated. > >Best, > >Peter D Bethke --------------------------------------------------------------------- Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail <[EMAIL PROTECTED]> To unsubscribe, e-mail <[EMAIL PROTECTED]> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php --------------------------------------------------------------------- Before posting, please check: http://www.mysql.com/manual.php (the manual) http://lists.mysql.com/ (the list archive) To request this thread, e-mail <[EMAIL PROTECTED]> To unsubscribe, e-mail <[EMAIL PROTECTED]> Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php