将基于累积值的多行映射到同一个 table 中的另一个引用行
Map multiple rows based on cumulative value to another reference row in the same table
我有 SQL table 如下:
PO
Delivery_Type
Scheduled_Line_Item
Date
Quantity
100
Scheduled Delivery
01
01/12/2021
100,000
100
Scheduled Delivery
02
07/12/2021
75,000
100
Actual Delivery
NULL
03/12/2021
50,000
100
Actual Delivery
NULL
12/12/2021
50,000
100
Actual Delivery
NULL
15/12/2021
20,000
100
Actual Delivery
NULL
31/12/2021
55,000
我正在尝试根据交货日期的顺序将实际交货项目映射到特定的 Scheduled_Line_Item,以产生如下结果:
PO
Delivery_Type
Scheduled_Line_Item
Date
Delivered_Qty
Scheduled_Qty
100
Actual Delivery
01
03/12/2021
50,000
100,000
100
Actual Delivery
01
12/12/2021
50,000
100,000
100
Actual Delivery
02
15/12/2021
20,000
75,000
100
Actual Delivery
02
31/12/2021
55,000
75,000
从上面可以看出,前两行映射到“Scheduled_Line_Item”“01”,因为Date 序列已经完成了Scheduled Delivery 中数量的前100,000。一旦“实际交付”完成了第一个“计划交付”,那么只有我们移动到下一个“计划交付”,这就是为什么接下来的两行映射到“Scheduled_Line_Item”“02”。
我找到了这个方法,但无法将其应用到我的要求中https://sqlsunday.com/2019/09/16/calculating-invoice-settlements/。
假设 PO
+ Date
对于 'Actual Delivery' 是唯一的,您可以匹配 运行 排除重复行的总数
with sd as (
select *, sum(Quantity) over(partition by PO order by date) s
from tbl
where Delivery_Type ='Scheduled Delivery'
),
ad as (
select *, sum(Quantity) over(partition by PO order by date) s
from tbl
where Delivery_Type ='Actual Delivery'
)
select top(1) with ties sd.*, ad.*
from sd
left join ad on ad.PO=sd.PO and sd.s>= ad.s
order by row_number() over(partition by ad.PO, ad.Date order by sd.date)
我有 SQL table 如下:
PO | Delivery_Type | Scheduled_Line_Item | Date | Quantity |
---|---|---|---|---|
100 | Scheduled Delivery | 01 | 01/12/2021 | 100,000 |
100 | Scheduled Delivery | 02 | 07/12/2021 | 75,000 |
100 | Actual Delivery | NULL | 03/12/2021 | 50,000 |
100 | Actual Delivery | NULL | 12/12/2021 | 50,000 |
100 | Actual Delivery | NULL | 15/12/2021 | 20,000 |
100 | Actual Delivery | NULL | 31/12/2021 | 55,000 |
我正在尝试根据交货日期的顺序将实际交货项目映射到特定的 Scheduled_Line_Item,以产生如下结果:
PO | Delivery_Type | Scheduled_Line_Item | Date | Delivered_Qty | Scheduled_Qty |
---|---|---|---|---|---|
100 | Actual Delivery | 01 | 03/12/2021 | 50,000 | 100,000 |
100 | Actual Delivery | 01 | 12/12/2021 | 50,000 | 100,000 |
100 | Actual Delivery | 02 | 15/12/2021 | 20,000 | 75,000 |
100 | Actual Delivery | 02 | 31/12/2021 | 55,000 | 75,000 |
从上面可以看出,前两行映射到“Scheduled_Line_Item”“01”,因为Date 序列已经完成了Scheduled Delivery 中数量的前100,000。一旦“实际交付”完成了第一个“计划交付”,那么只有我们移动到下一个“计划交付”,这就是为什么接下来的两行映射到“Scheduled_Line_Item”“02”。
我找到了这个方法,但无法将其应用到我的要求中https://sqlsunday.com/2019/09/16/calculating-invoice-settlements/。
假设 PO
+ Date
对于 'Actual Delivery' 是唯一的,您可以匹配 运行 排除重复行的总数
with sd as (
select *, sum(Quantity) over(partition by PO order by date) s
from tbl
where Delivery_Type ='Scheduled Delivery'
),
ad as (
select *, sum(Quantity) over(partition by PO order by date) s
from tbl
where Delivery_Type ='Actual Delivery'
)
select top(1) with ties sd.*, ad.*
from sd
left join ad on ad.PO=sd.PO and sd.s>= ad.s
order by row_number() over(partition by ad.PO, ad.Date order by sd.date)