List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:March 7 2003 5:28pm
Subject:Re: CASE statement within SUM()
View as plain text  
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.
>Peter D Bethke

CASE statement within SUM()Peter D Bethke7 Mar
  • Re: CASE statement within SUM()Paul DuBois7 Mar
Re: CASE statement within SUM()dpgirago7 Mar
Re: CASE statement within SUM()dpgirago7 Mar