简体   繁体   English

比较表 A 中一列和表 B 中另一列的值

[英]Compare values from one column in table A and another column in table B

I need to create a NeedDate column in the expected output. I will compare the QtyShort from Table B with QtyReceive from table A.我需要在预期的 output 中创建一个 NeedDate 列。我将比较表 B 中的 QtyShort 和表 A 中的 QtyReceive。

In the expected output, if QtyShort = 0, NeedDate = MaltDueDate.在预期的 output 中,如果 QtyShort = 0,NeedDate = MaltDueDate。

For the first row of table A, if 0 < QtyShort (in Table B) <= QtyReceive (=6), NeedDate = 10/08/2021 (DueDate from Table A).对于表 A 的第一行,如果 0 < QtyShort(在表 B 中)<= QtyReceive (=6),则 NeedDate = 10/08/2021(表 A 中的到期日期)。

If 6 < QtyShort <= 10 (QtyReceive), move to the second row, NeedDate = 10/22/2021 (DueDate from Table A).如果 6 < QtyShort <= 10 (QtyReceive),则移至第二行,NeedDate = 10/22/2021(表 A 中的到期日期)。

If 10 < QtyShort <= 20 (QtyReceive), move to the third row, NeedDate = 02/01/2022 (DueDate from Table A).如果 10 < QtyShort <= 20 (QtyReceive),移至第三行,NeedDate = 02/01/2022(表 A 中的到期日期)。

If QtyShort > QtyReceive (=20), NeedDate = 09/09/9999.如果 QtyShort > QtyReceive (=20),则 NeedDate = 09/09/9999。

This should continue in a loop until the last row on table B has been compared这应该继续循环,直到比较表 B 的最后一行

How could we do this?我们怎么能这样做? Any help will be appreciated.任何帮助将不胜感激。 Thank you in advance!先感谢您!

Table A表A

Item    DueDate      QtyReceive
A1      10/08/2021       6
A1      10/22/2021       10
A1      02/01/2022       20

Table B表B

Item    MatlDueDate    QtyShort
A1      06/01/2022         0
A1      06/02/2022         0
A1      06/03/2022         1
A1      06/04/2022         2
A1      06/05/2022         5
A1      06/06/2022         7
A1      06/07/2022         10
A1      06/08/2022         15  
A1      06/09/2022         25

Expected Output:预计 Output:

 Item    MatlDueDate    QtyShort     NeedDate
 A1      06/01/2022        0        06/01/2022
 A1      06/02/2022        0        06/02/2022
 A1      06/03/2022        1        10/08/2021
 A1      06/04/2022        2        10/08/2021
 A1      06/05/2022        5        10/08/2021
 A1      06/06/2022        7        10/22/2021
 A1      06/07/2022        10       10/22/2021
 A1      06/08/2022        15       02/01/2022 
 A1      06/09/2022        25       09/09/9999

Use OUTER APPLY() operator to find the minimum DueDate from TableA that is able to fulfill the QtyShort使用OUTER APPLY()运算符从TableA中找到能够满足QtyShort的最小DueDate

select b.Item, b.MatlDueDate, b.QtyShort,
       NeedDate = case when b.QtyShort = 0
                       then b.MatlDueDate
                       else isnull(a.DueDate, '9999-09-09')
                       end
from   TableB b
       outer apply
       (
           select DueDate = min(a.DueDate)
           from   TableA a
           where  a.Item = b.Item
           and    a.QtyReceive >= b.QtyShort
       ) a

Result:结果:

Item物品 MatlDueDate材料到期日 QtyShort数量不足 NeedDate需要日期
A1 A1 2022-06-01 2022-06-01 0 0 2022-06-01 2022-06-01
A1 A1 2022-06-02 2022-06-02 0 0 2022-06-02 2022-06-02
A1 A1 2022-06-03 2022-06-03 1 1个 2021-10-08 2021-10-08
A1 A1 2022-06-04 2022-06-04 2 2个 2021-10-08 2021-10-08
A1 A1 2022-06-05 2022-06-05 5 5个 2021-10-08 2021-10-08
A1 A1 2022-06-06 2022-06-06 7 7 2021-10-22 2021-10-22
A1 A1 2022-06-07 2022-06-07 10 10 2021-10-22 2021-10-22
A1 A1 2022-06-08 2022-06-08 15 15 2022-02-01 2022-02-01
A1 A1 2022-06-09 2022-06-09 25 25 9999-09-09 9999-09-09

db<>fiddle demo 数据库<>小提琴演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM