给出下表:
列
- 交易开始日期
- 交易结束日期 量
There is a table:
起始日期 | 结束日期 | 金额 | 01.01.2020 | 01.01.2021 | 1 |
---|---|---|
15.04.2020 | 15.04.2021 | 3 |
您可以使用条件聚合或过滤求和函数如下:
SELECT
SUM(CASE WHEN '02.05.2020' >= End_Date THEN Amount ELSE 0 END) AS Date1,
SUM(CASE WHEN '02.05.2020' >= End_Date OR '02.02.2021' >= End_Date THEN Amount ELSE 0 END) AS Date2
FROM table_name
或:
SELECT
SUM(Amount) FILTER (WHERE '02.05.2020' >= End_Date) AS Date1,
SUM(Amount) FILTER (WHERE '02.05.2020' >= End_Date OR '02.02.2021' >= End_Date) AS Date2
FROM table_name
查看演示