1

我有这张桌子:

Quarter | Col A | Col B | Col C | Col D | Col E
--------+-------+-------+-------+-------+-------
1       | aaa   | 1     | 2     | 3     | 4
2       | aaa   | 1.1   | 2.2   | 3.3   | 4.4
3       | aaa   | 1.11  | 2.22  | 3.33  | 4.44
4       | aaa   | 1.111 | 2.222 | 3.333 | 4.444
1       | bbb   | 5     | 6     | 7     | 8
2       | bbb   | 5.5   | 6.6   | 7.7   | 8.8
3       | bbb   | 5.55  | 6.66  | 7.77  | 8.88
4       | bbb   | 5.555 | 6.666 | 7.777 | 8.888

我想得到这个:

Col A | Sum_Diagonally_Across_Quarters
------+-------------------------------
aaa   | 1 + 2.2 + 3.33 + 4.444
bbb   | 5 + 6.6 + 7.77 + 8.888

这个想法是将第 1 季度的 Col B 与第 2 季度的 Col C 相加...并按 Col A 分组

4

1 回答 1

2

很奇怪,但是可以使用条件聚合:

select cola,
       sum(case when quarter = 1 then colb
                when quarter = 2 then colc
                when quarter = 3 then cold
                when quarter = 4 then cole
           end)                
from t
group by cola
于 2019-11-18T13:35:36.883 回答