SQL Column Sum - using if statement to double rows values before summing the column
Four column table - id (int), double1 (bit), double2 (bit), score (int)
Want 开发者_如何学编程to write a query the returns the SUM() of the score column, grouped by id, where each row score can be changed based on the two double columns. So if the score is 10 and double1 and/or double2 columns are true, the row score is doubled once or twice.
SELECT id, SUM(score * (double1 + 1) * (double2 + 1))
FROM tbl
GROUP BY id
(Edited as explicit cast not required. int has higher precedence)
精彩评论