【问题标题】:Matching column values across multiple rows跨多行匹配列值
【发布时间】:2020-02-07 15:03:25
【问题描述】:

我有一个与此类似的表格,需要计算同一天、未来日期或未完成的订单数量。 A700 是预提交的,A18 是提交的。它们在 UniqueID 上匹配。我想我需要使用 JOIN 但不知道该怎么做。在我努力解决问题时,任何指点都非常感谢。

AuditID  |  ActivityDateTime  |  ProductCode  |  UniqueID  |  DeliveryDate
A700            2020-01-01         111111       1234567890      NULL
A700            2020-01-01         111111       2222222222      NULL
A700            2020-01-01         222222       9999999999      NULL
A700            2020-01-01         333333       5555555555      NULL

A18     2020-01-01         112233       1234567890      2020-01-01
A18     2020-01-01         112233       2222222222      2020-01-03
A18     2020-01-01         333333       5555555555      2020-01-03

结果:

ProductCode  |  SameDay  |  FutureDay
112233             1            1
333333             0            1
222222             0            0

【问题讨论】:

    标签: sql join count match


    【解决方案1】:

    在我看来,这就像带有聚合的自联接。像这样的:

    select t700.productcode,
           sum(case when t700.ActivityDateTime = t18.DeliveryDate then 1 else 0 end) as sameday,
           sum(case when t700.ActivityDateTime < t18.DeliveryDate then 1 else 0 end) as futureday,
    from t t700 join
         t t18
         on t700.uniqueid = t18.uniqueid and
            t700.auditid = 'A700' and
            t18.auditid = 'A18'
    group by t700.productcode;
    

    ActivityDateTime 列似乎有一个时间组件。如果是这样,您可能需要调整相同日期和未来日期的比较。

    【讨论】:

    • 对不起,戈登 - 被转移到别的地方,只是回到这个。这极大地帮助并解决了我的问题。非常感谢。 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-31
    • 2020-08-30
    • 1970-01-01
    • 1970-01-01
    • 2021-06-13
    • 1970-01-01
    相关资源
    最近更新 更多