我有这些表:当 KEY3 = X 时,我需要使用相同的 KEY1 和 KEY2 “汇总”所有值
| KEY1 | VALUE1 | KEY2 | VALUE2 | KEY3 |
| 1 | 9 | 2 | 2 | x |
| 1 | 10 | 2 | 3 | x |
| 2 | 5 | 1 | 8 | x |
| 2 | 5 | 1 | 9 | y |
SELECT key1 as k1, key2 as k2 FROM table1 WHERE key3 = x
对于这些查询的每个结果,我需要做:
SELECT
COALESCE(SUM(case when key1 = k1 then totalamount1 else 0 end),0) +
COALESCE(SUM(case when key2 = k2 then totalamount2 else 0 end),0) as total
FROM table1
WHERE key3 = x
Y 需要这些输出:
Total
id 1: 27
id 2: 10
我怎样才能加入这两个查询?
谢谢!