Oracle SQL:如何对每行的每 x 个后续行求和
Oracle SQL: How can I sum every x number of subsequent rows for each row
我有一个数据 table 看起来像这样:
|Contract Date | Settlement_Prcie |
|--------------|------------------|
| 01/10/2020 | 50 |
|--------------|------------------|
| 01/11/2020 | 10 |
|--------------|------------------|
| 01/01/2021 | 20 |
|--------------|------------------|
| 01/02/2021 | 30 |
|--------------|------------------|
| 01/03/2021 | 50 |
|--------------|------------------|
我想编写一个查询,对下面的每两行求和...例如,在合同日期为 01/10/2020 的第一行,总和列将加上 10 和 20 得到结果30. 下一行,总和列将 20 和 30 相加得到 40,依此类推。结果 table 的结果如下所示:
|Contract Date | Settlement_Prcie | Sum Column |
|--------------|------------------|------------|
| 01/10/2020 | 50 | 30
|--------------|------------------|------------|
| 01/11/2020 | 10 | 50
|--------------|------------------|------------|
| 01/01/2021 | 20 | 80
|--------------|------------------|------------|
| 01/02/2021 | 30 |
|--------------|------------------|
| 01/03/2021 | 50 |
|--------------|------------------|
任何人都可以帮助我进行查询以不仅针对 2 个后续行而且针对 x 个后续行执行此操作。
到目前为止,我已经尝试过使用 SUM (Settlement_Price) Over (order by Contract_date Rows between 3 preceding and current row) - Current row
当然不行,但那是我所能做的。
SUM (Settlement_Price) Over (order by Contract_date Rows between 1 following and 2 following)
可以使用SUM
解析函数:
SELECT contract_date,
settlement_price,
CASE COUNT(*) OVER (
ORDER BY contract_date ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING
)
WHEN 2
THEN SUM( settlement_price ) OVER (
ORDER BY contract_date ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING
)
END AS sum_column
FROM table_name;
或者您可以使用 LEAD
:
SELECT contract_date,
settlement_price,
LEAD( settlement_price, 1 , NULL ) OVER ( ORDER BY contract_date )
+ LEAD( settlement_price, 2 , NULL ) OVER ( ORDER BY contract_date )
AS sum_column
FROM table_name;
所以,对于测试数据:
CREATE TABLE table_name ( contract_date, settlement_price ) AS
SELECT DATE '2020-10-01', 50 FROM DUAL UNION ALL
SELECT DATE '2020-11-01', 10 FROM DUAL UNION ALL
SELECT DATE '2020-12-01', 20 FROM DUAL UNION ALL
SELECT DATE '2021-01-01', 30 FROM DUAL UNION ALL
SELECT DATE '2021-02-01', 50 FROM DUAL;
两个查询输出:
CONTRACT_DATE | SETTLEMENT_PRICE | SUM_COLUMN
:------------ | ---------------: | ---------:
01-OCT-20 | 50 | 30
01-NOV-20 | 10 | 50
01-DEC-20 | 20 | 80
01-JAN-21 | 30 | null
01-FEB-21 | 50 | null
db<>fiddle here
我有一个数据 table 看起来像这样:
|Contract Date | Settlement_Prcie |
|--------------|------------------|
| 01/10/2020 | 50 |
|--------------|------------------|
| 01/11/2020 | 10 |
|--------------|------------------|
| 01/01/2021 | 20 |
|--------------|------------------|
| 01/02/2021 | 30 |
|--------------|------------------|
| 01/03/2021 | 50 |
|--------------|------------------|
我想编写一个查询,对下面的每两行求和...例如,在合同日期为 01/10/2020 的第一行,总和列将加上 10 和 20 得到结果30. 下一行,总和列将 20 和 30 相加得到 40,依此类推。结果 table 的结果如下所示:
|Contract Date | Settlement_Prcie | Sum Column |
|--------------|------------------|------------|
| 01/10/2020 | 50 | 30
|--------------|------------------|------------|
| 01/11/2020 | 10 | 50
|--------------|------------------|------------|
| 01/01/2021 | 20 | 80
|--------------|------------------|------------|
| 01/02/2021 | 30 |
|--------------|------------------|
| 01/03/2021 | 50 |
|--------------|------------------|
任何人都可以帮助我进行查询以不仅针对 2 个后续行而且针对 x 个后续行执行此操作。
到目前为止,我已经尝试过使用 SUM (Settlement_Price) Over (order by Contract_date Rows between 3 preceding and current row) - Current row
当然不行,但那是我所能做的。
SUM (Settlement_Price) Over (order by Contract_date Rows between 1 following and 2 following)
可以使用SUM
解析函数:
SELECT contract_date,
settlement_price,
CASE COUNT(*) OVER (
ORDER BY contract_date ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING
)
WHEN 2
THEN SUM( settlement_price ) OVER (
ORDER BY contract_date ROWS BETWEEN 1 FOLLOWING AND 2 FOLLOWING
)
END AS sum_column
FROM table_name;
或者您可以使用 LEAD
:
SELECT contract_date,
settlement_price,
LEAD( settlement_price, 1 , NULL ) OVER ( ORDER BY contract_date )
+ LEAD( settlement_price, 2 , NULL ) OVER ( ORDER BY contract_date )
AS sum_column
FROM table_name;
所以,对于测试数据:
CREATE TABLE table_name ( contract_date, settlement_price ) AS
SELECT DATE '2020-10-01', 50 FROM DUAL UNION ALL
SELECT DATE '2020-11-01', 10 FROM DUAL UNION ALL
SELECT DATE '2020-12-01', 20 FROM DUAL UNION ALL
SELECT DATE '2021-01-01', 30 FROM DUAL UNION ALL
SELECT DATE '2021-02-01', 50 FROM DUAL;
两个查询输出:
CONTRACT_DATE | SETTLEMENT_PRICE | SUM_COLUMN :------------ | ---------------: | ---------: 01-OCT-20 | 50 | 30 01-NOV-20 | 10 | 50 01-DEC-20 | 20 | 80 01-JAN-21 | 30 | null 01-FEB-21 | 50 | null
db<>fiddle here