SQL - 如何对不同行求和,同时第一行有不同的方程式
SQL - how to sum across different rows while having a different equation for the first row
我无法弄清楚如何对不同的行求和,但使用每个分组的第一行作为起点。
我有一个 table 看起来像这样 :
+------+------+----------+---------------+----------+-------+
| Dim1 | Dim2 | Date | beg_Inventory | purchase | sales |
+------+------+----------+---------------+----------+-------+
| x | y | 1/1/2019 | 100 | 50 | 20 |
| x | y | 1/2/2019 | | 70 | 80 |
| x | y | 1/3/2019 | | 40 | 60 |
| x | y | 1/4/2019 | | 30 | 50 |
| x | y | 1/5/2019 | | 100 | 10 |
| x | z | 1/1/2019 | 65 | 10 | 50 |
| x | z | 1/2/2019 | | 20 | 5 |
| x | z | 1/3/2019 | | 40 | 5 |
+------+------+----------+---------------+----------+-------+
我想要这样的结果:
+------+------+----------+---------------+----------+-------+------------+
| Dim1 | Dim2 | Date | beg_Inventory | purchase | sales | ending_inv |
+------+------+----------+---------------+----------+-------+------------+
| x | y | 1/1/2019 | 100 | 50 | 20 | 130 |
| x | y | 1/2/2019 | | 70 | 80 | 120 |
| x | y | 1/3/2019 | | 40 | 60 | 100 |
| x | y | 1/4/2019 | | 30 | 50 | 80 |
| x | y | 1/5/2019 | | 100 | 10 | 170 |
| x | z | 1/1/2019 | 65 | 10 | 50 | 25 |
| x | z | 1/2/2019 | | 20 | 5 | 40 |
| x | z | 1/3/2019 | | 40 | 5 | 75 |
+------+------+----------+---------------+----------+-------+------------+
期末库存为 beg_inventory + 采购 - 销售,对于每个 dim1 和 dim2 分组的第一行,即第一行为 100+50-20=130
但是,在第2行中,它必须使用我们计算的130并使用下一行的购买和销售得到130 + 70-80 = 120等等,按dim1和dim2分组并按日期排序。
谢谢。
尝试使用 first_value()
获取 window 中 beg_inventory
的第一个值,然后添加 windowed sum()
的采购和销售
SELECT dim1,
dim2,
date,
beg_inventory,
purchase,
sales,
first_value(beg_inventory) OVER (PARTITION BY dim1,
dim2
ORDER BY date)
+
sum(purchase - sales) OVER (PARTITION BY dim1,
dim2
ORDER BY date) ending_inv
FROM elbat
ORDER BY dim1,
dim2,
date;
所有行的公式对我来说都一样:
SELECT t.*,
SUM(COALESCE(beg_Inventory, 0) + purchase - sales) OVER (PARTITION BY dim1, dim2 ORDER BY date) as ending_inv
FROM t
ORDER BY dim1, dim2, date;
我无法弄清楚如何对不同的行求和,但使用每个分组的第一行作为起点。
我有一个 table 看起来像这样 :
+------+------+----------+---------------+----------+-------+
| Dim1 | Dim2 | Date | beg_Inventory | purchase | sales |
+------+------+----------+---------------+----------+-------+
| x | y | 1/1/2019 | 100 | 50 | 20 |
| x | y | 1/2/2019 | | 70 | 80 |
| x | y | 1/3/2019 | | 40 | 60 |
| x | y | 1/4/2019 | | 30 | 50 |
| x | y | 1/5/2019 | | 100 | 10 |
| x | z | 1/1/2019 | 65 | 10 | 50 |
| x | z | 1/2/2019 | | 20 | 5 |
| x | z | 1/3/2019 | | 40 | 5 |
+------+------+----------+---------------+----------+-------+
我想要这样的结果:
+------+------+----------+---------------+----------+-------+------------+
| Dim1 | Dim2 | Date | beg_Inventory | purchase | sales | ending_inv |
+------+------+----------+---------------+----------+-------+------------+
| x | y | 1/1/2019 | 100 | 50 | 20 | 130 |
| x | y | 1/2/2019 | | 70 | 80 | 120 |
| x | y | 1/3/2019 | | 40 | 60 | 100 |
| x | y | 1/4/2019 | | 30 | 50 | 80 |
| x | y | 1/5/2019 | | 100 | 10 | 170 |
| x | z | 1/1/2019 | 65 | 10 | 50 | 25 |
| x | z | 1/2/2019 | | 20 | 5 | 40 |
| x | z | 1/3/2019 | | 40 | 5 | 75 |
+------+------+----------+---------------+----------+-------+------------+
期末库存为 beg_inventory + 采购 - 销售,对于每个 dim1 和 dim2 分组的第一行,即第一行为 100+50-20=130 但是,在第2行中,它必须使用我们计算的130并使用下一行的购买和销售得到130 + 70-80 = 120等等,按dim1和dim2分组并按日期排序。
谢谢。
尝试使用 first_value()
获取 window 中 beg_inventory
的第一个值,然后添加 windowed sum()
的采购和销售
SELECT dim1,
dim2,
date,
beg_inventory,
purchase,
sales,
first_value(beg_inventory) OVER (PARTITION BY dim1,
dim2
ORDER BY date)
+
sum(purchase - sales) OVER (PARTITION BY dim1,
dim2
ORDER BY date) ending_inv
FROM elbat
ORDER BY dim1,
dim2,
date;
所有行的公式对我来说都一样:
SELECT t.*,
SUM(COALESCE(beg_Inventory, 0) + purchase - sales) OVER (PARTITION BY dim1, dim2 ORDER BY date) as ending_inv
FROM t
ORDER BY dim1, dim2, date;