将已作废的收据与原始收据匹配:
我有一个transaction
看起来像这样的表:
transaction_start store_no item_no amount post_voided
2021-02-28 08:00:00 001 101 45 N
2021-02-28 08:00:00 001 105 25 N
2021-02-28 08:00:00 001 109 40 N
2021-03-01 10:00:00 001 101 45 N
2021-03-01 10:00:00 001 105 25 N
2021-03-01 10:00:00 001 109 40 N
2021-03-01 10:02:00 002 101 45 N
2021-03-01 10:02:00 002 105 25 N
2021-03-01 10:02:00 002 109 40 N
2021-03-01 10:04:00 001 101 45 N
2021-03-01 10:05:00 002 103 35 N
2021-03-01 10:05:00 002 135 20 N
2021-03-01 10:08:00 001 140 2 N
2021-03-01 10:11:00 001 101 -45 Y
2021-03-01 10:11:00 001 105 -25 Y
2021-03-01 10:11:00 001 109 -40 Y
每张收据/购买都是transaction_start
和的组合store_no
。每张收据可以有一个或多个(以及由变量item_no
给出的相应价格)。amount
如果收据有两个或更多项目,则重复transaction_start
和值。store_no
所以在上表中,总共可以观察到 7 个收据:收据 1 由transaction_start
= '2021-02-28 08:00:00' 和store_no
= '001' 给出,包含以下项目:101、105 和 109 ; 收据 2 by transaction_start
= '2021-03-01 10:00:00' and store_no
= '001',包含以下项目:101、105 和 109,依此类推。
最后一张收据(transaction_start
='2021-03-01 10:11:00' and store_no
='001')是第二张收据(transaction_start
='2021-03-01 10:00:00' and store_no
=' 001')无效的帖子)。后作废指示由列和值post_voided
= 'Y' 给出。
我的目标是更改原始日期transaction_start
时间的post_voided
= 'Y' 收据。transaction_start
原始收据是指所有item_no
和(负)amount
来自后作废收据(即post_voided
= 'Y')必须与最接近的(即相等或更低)transaction_start
、包含相同、(正)store_no
的收据匹配,而不是后作废( oe = 'N')。item_no
amount
post_voided
期望的输出:
transaction_start store_no item_no amount post_voided
2021-02-28 08:00:00 001 101 45 N
2021-02-28 08:00:00 001 105 25 N
2021-02-28 08:00:00 001 109 40 N
2021-03-01 10:00:00 001 101 45 N
2021-03-01 10:00:00 001 105 25 N
2021-03-01 10:00:00 001 109 40 N
2021-03-01 10:02:00 002 101 45 N
2021-03-01 10:02:00 002 105 25 N
2021-03-01 10:02:00 002 109 40 N
2021-03-01 10:04:00 001 101 45 N
2021-03-01 10:05:00 002 103 35 N
2021-03-01 10:05:00 002 135 20 N
2021-03-01 10:08:00 001 140 2 N
2021-03-01 10:00:00 001 101 -45 Y
2021-03-01 10:00:00 001 105 -25 Y
2021-03-01 10:00:00 001 109 -40 Y
这里是表格的链接:https ://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=06afe841d89c4ecbf600436433cd1c5d
提前致谢!