SQL - 获取三个表之间两行之间的日期差异

时间:2016-05-14 03:59:31

标签: sql

我已经搜索了论坛,但无法找到合适的解决方案。

我有两张表,其中包含以下信息:

-TableA -

   Id   |   Created
  11111 | 2016-01-01
  22222 | 2016-02-02
  33333 | 2016-03-03

-TableB -

   Id   |   Created   |   Comment   
  11111 | 2016-01-01  | Blah Blah Blah
  11111 | 2016-01-02  | Blah Blah Blah
  11111 | 2016-01-15  | Blah Blah Blah
  11111 | 2016-01-17  | Blah Blah Blah
  22222 | 2016-02-02  | Blah Blah Blah
  22222 | 2016-02-05  | Blah Blah Blah
  22222 | 2016-02-09  | Blah Blah Blah
  33333 | 2016-03-03  | Blah Blah Blah
  33333 | 2016-03-14  | Blah Blah Blah

TableA是主表(它有很多其他字段,但重要的是ID和创建的日期字段),而TableB是一个与TableA绑定的注释表。

我尝试做的是计算TableB中两行之间的时差,然后隔离创建记录的第一行。我认为最好的方法是使用TableA提供确定的创建日期,并在获得所有计算出的时间差后以某种方式对TableB使用。

我已经为TableB写了一个合理的查询,以便给出计算的日期差异:

SELECT C1.Id,
       C1.Created,
       MIN(C2.Created) AS Created2,
       DATEDIFF(C1.Created, MIN(C2.Created) AS DaysDiff
       FROM TableB C1
            LEFT JOIN TableB C2
            ON C1.Id = C2.Id
            AND C2.Created > C1.Created
       GROUP BY C1.Id, C1.Created

-TableB查询 -

   Id   |   Created   |  Created2   |    DaysDiff
  11111 | 2016-01-01  | 2016-01-02  |       1
  11111 | 2016-01-02  | 2016-01-15  |      13
  11111 | 2016-01-15  | 2016-01-17  |       2
  11111 | 2016-01-17  |             |       
  22222 | 2016-02-02  | 2016-02-05  |       3
  22222 | 2016-02-05  | 2016-02-09  |       4
  22222 | 2016-02-09  |             |        
  33333 | 2016-03-03  | 2016-03-14  |      11
  33333 | 2016-03-14  |             |        

但我需要更进一步,只获得最早的创建记录,所以它看起来像这样:

   Id   |   Created   |  Created2   |    DaysDiff
  11111 | 2016-01-01  | 2016-01-02  |       1
  22222 | 2016-02-02  | 2016-02-05  |       3
  33333 | 2016-03-03  | 2016-03-14  |      11

我非常确定我需要在这里再做一次JOIN,但是我所做的任何JOIN通常会在我没有记录的地方结束,或者我只获得Id和Created列而没有别的。< / p>

感谢您的帮助!

4 个答案:

答案 0 :(得分:1)

要获得预期结果,您应该使用:

SELECT C1.Id,
       C1.Created,
       MIN(C2.Created) AS Created2,
       DATEDIFF(C1.Created, MIN(C2.Created)) AS DaysDiff
FROM (select id, min(created) created from TableB group by id) C1
    JOIN TableB C2
    ON C1.Id = C2.Id
    AND C2.Created > C1.Created
GROUP BY C1.Id, C1.Created
;

在开始时,我认为临时C1表应该是tableA,而不是tableB中的子查询选择min(已创建)。如果确实如此,则将该行更改为FROM (....) C1From tableA C1

答案 1 :(得分:0)

尝试这样的事情:

select A.id, A.created, B.created, DATEDIFF(B.created, A.created) AS DaysDiff
  from TableA A
  join TableB B on B.id = A.id and
  B.created = (select min(created) from TableB where
            created > A.created)

这是一个尝试它的SQLFiddle:http://www.sqlfiddle.com/#!9/859113/3

答案 2 :(得分:0)

使用TableA加入TableB而不是使用TableB两次,这样你就可以摆脱不需要的最小创建日期

SELECT
    TableA.Id,
    TableA.Created,
    MIN(TableB.Created) AS Created2,
    DATEDIFF(MIN(TableB.Created), TableA.Created) AS DaysDiff
FROM TableA 
     JOIN TableB 
        ON TableA.Id = TableB.Id
            AND TableA.Created < TableB.Created
GROUP BY
    TableA.Id,
    TableA.Created

经过测试和工作+在另一个SELECT中不需要SELECT http://www.sqlfiddle.com/#!9/859113/8

对于我重复使用的小提琴@xfiddler来说:)

答案 3 :(得分:0)

作为T-SQL选项,可以使用交叉应用,例如

|   id |    created |    created | DaysDiff |
|------|------------|------------|----------|
| 1111 | 2016-01-01 | 2016-01-02 |        1 |
| 2222 | 2016-02-02 | 2016-02-05 |        3 |
| 3333 | 2016-03-03 | 2016-03-14 |       11 |


SELECT
      A.id
    , A.created
    , B.created
    , DATEDIFF(day,A.created,B.created) AS DaysDiff
FROM TableA A
      CROSS APPLY (
            SELECT
                  MIN(created) created
            FROM TableB
            WHERE created > A.created
                  AND TableB.id = A.id
      ) B

请参阅此SQL Fiddle