【问题标题】:Join tables on dates when dates are different在日期不同的日期加入表格
【发布时间】:2019-01-16 17:45:53
【问题描述】:

所以基本上我想知道如果它们不同,我如何在日期上加入两个表。第一个表是我的主表,其中包括我所有购买过商品的客户。购买日期是过去的单点:

Customers
    +--------+----------+-------+------------+
    | custid | Quantity | Price | ReportDate |
    +--------+----------+-------+------------+
    | 371965 |       12 |     2 | 9/1/2016   |
    | 371965 |        2 |     5 | 2/25/2018  |
    | 377958 |       45 |     3 | 9/1/2016   |
    | 270723 |       12 |  1.25 | 5/1/2014   |
    | 270723 |    10.86 |  1.25 | 6/1/2014   |
    | 270723 |    12.29 |   1.3 | 7/1/2014   |
    | 270723 |    12.29 |   1.4 | 9/15/2016  |
    +--------+----------+-------+------------+

因此,我想将我的客户表与折扣一起加入表中。折扣生效的时间基本上是直到发布新折扣的整个期间:

Discounts
    +----+-----------+----------+
    | id | startdate | discount |
    +----+-----------+----------+
    |  1 | 7/18/2013 |      0.1 |
    |  2 | 1/10/2014 |     0.25 |
    |  3 | 7/11/2016 |     0.11 |
    |  4 | 9/14/2016 |     0.12 |
    |  5 | 1/12/2017 |     0.15 |
    |  6 | 2/6/2017  |     0.22 |
    |  7 | 6/28/2017 |     0.09 |
    +----+-----------+----------+

所以我的目标是链接这两个表并查看哪个购买日期落在适当的折扣区间内。这将是我的目标:

+--------+----------+-------+------------+----+-----------+----------+
| custid | Quantity | Price | ReportDate | id | startdate | discount |
+--------+----------+-------+------------+----+-----------+----------+
| 371965 |       12 |     2 | 9/1/2016   |  3 | 7/11/2016 |     0.11 |
| 371965 |        2 |     5 | 2/25/2018  |  7 | 6/28/2017 |     0.09 |
| 377958 |       45 |     3 | 9/1/2016   |  3 | 7/11/2016 |     0.11 |
| 270723 |       12 |  1.25 | 5/1/2014   |  2 | 41649     |     0.25 |
| 270723 |    10.86 |  1.25 | 6/1/2014   |  2 | 1/10/2014 |     0.25 |
| 270723 |    12.29 |   1.3 | 7/1/2014   |  2 | 1/10/2014 |     0.25 |
| 270723 |    12.29 |   1.4 | 9/15/2016  |  4 | 9/14/2016 |     0.12 |
+--------+----------+-------+------------+----+-----------+----------+

【问题讨论】:

    标签: sql sql-server date join period


    【解决方案1】:

    您可以使用 OUTER APPLY 选择报告日期之前存在的 TOP 1 Discount,按日期降序排列以获得最近的操作

      SELECT C1.*,DQ.*  FROM CUSTOMERS C1 OUTER APPLY 
                     (SELECT TOP 1 D.* FROM Discounts D WHERE C1.ReportDate >= D.startDate  
                                           ORDER BY D.StartDate DESC) DQ
    

    【讨论】:

      【解决方案2】:

      你可以这样加入:

      select c.*, d.*
      from customers c inner join discounts d
      on d.startdate = (select max(startdate) from discounts where startdate <= c.reportdate)
      

      demo

      【讨论】:

        【解决方案3】:

        只是另一种选择

        ;with cte as (
        Select A.* 
              ,B.*
              ,RN = row_number() over (Partition by custid,reportdate order by startdate desc)
         From Customers A
         Join Discounts B on B.startdate<=A.ReportDate
        ) 
        Select * 
         From  cte 
         Where RN=1
        

        dbFiddle

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-06-12
          • 1970-01-01
          • 2015-01-25
          • 2016-04-02
          • 1970-01-01
          • 2021-02-08
          • 1970-01-01
          相关资源
          最近更新 更多