SQL连接2个具有日期差异的表

时间:2019-05-26 16:17:14

标签: sql database

我有2张桌子,其中一张是订单,另一张是收据。我面临的问题是他们没有UID,他们唯一可以加入的就是日期。问题在于,日期是相同的,但它们之间的时间差约为30秒。我的问题是,有什么办法可以加入这两个表?

因此,第一个表的格式如下

|   date        | order | price | 
|1/1/13 06:05:32|  tea  |   3   |
|1/2/13 07:04:24| coffee|   2   |
|4/3/13 13:31:23|  tea  |   3   |

第二张表具有这种格式

|   date        | order | quantity | 
|1/1/13 06:05:42|  tea  |   3      |
|1/2/13 07:04:54| coffee|   2      |
|4/3/13 13:31:56|  tea  |   3      |

我想要的输出是

|   date        | order | quantity |  price | 
|1/1/13 06:05:42|  tea  |   3      |    3   |
|1/2/13 07:04:54| coffee|   2      |    2   |
|4/3/13 13:31:56|  tea  |   3      |    3   |

基本上,我的目标是合并这2个表,以便我可以看到它们之间的区别,但是我不知道如何在没有唯一ID的情况下将它们加入,请帮助大家

4 个答案:

答案 0 :(得分:1)

这是一个解决方案(在SQLite3上尝试过,但是如果替换为相应的date函数,则对于其他DBMS也是如此)

create table orders([date],[order],price);
insert into orders values
('2013-01-01 06:05:32', 'tea',  3),
('2013-01-02 07:04:24','coffee',2),
('2013-04-03 13:31:23', 'tea',  3);

create table receipts([date],[order],quantity);
insert into receipts values
('2013-01-01 06:05:42', 'tea',  3),
('2013-01-02 07:04:54','coffee',2),
('2013-04-03 13:31:56', 'tea',  3);

-- My desired output is
--
-- |   date        | order | quantity |  price |
-- |1/1/13 06:05:42|  tea  |   3      |    3   |
-- |1/2/13 07:04:54| coffee|   2      |    2   |
-- |4/3/13 13:31:56|  tea  |   3      |    3   |

select r.[date],[order],quantity,price
  from orders o join receipts r using([order])
  where r.date > o.date
    and r.date < datetime(o.date,'+40 seconds')

甚至:

select r.[date],[order],quantity,price
  from orders o join receipts r using([order])
  where r.date between o.date and datetime(o.date,'+40 seconds')

我用了40秒(您说大约30秒,但是您的示例输出抓住了33秒的时差)。根据需要进行调整。我还假设(根据您的示例)订单总是在收据之前出现。

答案 1 :(得分:0)

在sql server中,您可以执行以下操作:

select *
from ( select *, convert(date, getdate()) as d1 from t1) as t1,
( select *, convert(date, getdate()) as d2 from t2) as t2
where t1.d1 = t2.d2
and t1.order = t2.order

这里的要点是将日期和时间转换为仅日期并连接两个表

答案 2 :(得分:0)

您可以按日期值枚举并加入该日期值:

select t2.*, t1.*
from (select t1.*, row_number() over (partition by product order by date) as seqnum
      from t1
     ) t1 join
     (select t2.*, row_number() over (partition by product order by date) as seqnum
      from t2
     ) t2 
     on t1.product = t2.product and
        t1.seqnum = t2.seqnum;

您还可以加入时差:

select t2.*, t1.*
from t2 join
     t1
     on t2.product = t1.product and
        t2.date >= dateadd(second, -30, t1.date) and
        t2.date < dateadd(second, 30, t1.date);

或使用apply来获取最接近t2中时间的时间:

select t2.*, t1.*
from t2 outer appy
     (select top (1) t1.*
      from t1
      where t1.product = t2.product and
            t1.date <= t2.date and
            t1.date > dateadd(second, -60, t2.date)  -- just to keep within a range
      order by t1.date desc
     ) t1;

答案 3 :(得分:0)

如果可以确保两个表中的数据顺序始终相同,我的意思是表1中的行号X始终属于表2中的行号X,则可以使用row_number并使用ROW_NUMBER联接两个表。这可能是一个选择。

SELECT A.Date, A.Order, A.Price, B.quantity
FROM 
(
    SELECT  Date, Order, Price,
    ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) RN
    FROM Table1
)A
INNER JOIN 
(
        SELECT  Date, Order, quantity,
        ROW_NUMBER() OVER (ORDER BY (SELECT NULL)) RN
)B ON A.RN = B.RN