Oracle SQL: How can I sum every x number of subsequent rows for each row

732 views Asked by At

I have a data table that looks like this:

|Contract Date | Settlement_Prcie |
|--------------|------------------|
| 01/10/2020   |         50       |
|--------------|------------------|
| 01/11/2020   |         10       |
|--------------|------------------|
| 01/01/2021   |         20       |
|--------------|------------------|
| 01/02/2021   |         30       |
|--------------|------------------|
| 01/03/2021   |         50       |
|--------------|------------------|

I would like to write a query that sums every two rows beneath ... For example, On the first row with contract date 01/10/2020, the sum column would add 10 and 20 to give a result of 30. The next row, the sum column would add 20 and 30 to give 40 and so on. The resulting table of results would look like this:

|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       |
|--------------|------------------|

Could anyone please help me with the query to do this not just for 2 subsequent rows but for x subsequent rows.

So far I had tried using a SUM (Settlement_Price) Over (order by Contract_date Rows between 3 preceding and current row) - Current row of course was not ok, but that is as far as I had gone.

2

There are 2 answers

1
MT0 On BEST ANSWER

You can use the SUM analytic function:

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;

Or you can use 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;

So, for the test data:

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;

Both queries output:

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

0
Sayan Malakshinov On
SUM (Settlement_Price) Over (order by Contract_date Rows between 1 following and 2 following)