请参阅我的第一个问题,我的话题: SQL:分区了两列
我有如下表:
----------------------------------
| No1 | No2 | Amount| Timestamp
----------------------------------
| A | B | 10 | 01.01.2018
| C | D | 20 | 02.01.2018
| B | A | 30 | 03.01.2018
| D | C | 40 | 04.01.2018
----------------------------------
我此刻的结果如下:
-----------------------------------------------------
| No1 | No2 | Sum(Amount) over partition | Timestamp
-----------------------------------------------------
| A | B | 40 | 01.01.2018
| C | D | 60 | 02.01.2018
| B | A | 40 | 03.01.2018
| D | C | 60 | 04.01.2018
-----------------------------------------------------
与SQL(从Vamsi Prabhala的回答第一个问题):
select no1,no2,sum(amount) over(partition by least(no1,no2),greatest(no1,no2)) as total, timestamp
from tbl
现在对我来说,问题是我怎么可以添加行的结果类似:
----------------------------------------------------
| No1 | No2 | Sum(Amount) over partition | Timestamp
----------------------------------------------------
| A | B | 40 (optional) | 01.01.2018
| B | A | 40 (optional) | 02.01.2018
| AB |(NULL)| 40 |
| C | D | 60 (optional) | 03.01.2018
| D | C | 60 (optional) | 04.01.2018
| CD |(NULL)| 60 |
----------------------------------------------------
请注意,可以存在与例如值(1号= A,NO 2 = B)的多个行
更新:加入时间戳列更具体的我想达到的目标