开发者

SQL Column Sum - using if statement to double rows values before summing the column

开发者 https://www.devze.com 2023-01-12 06:03 出处:网络
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 ch

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)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号