简体   繁体   English

在2个日期之间的日期联接2个表

[英]Join 2 tables on date between 2 dates

I am trying to find a location of a customer on a given date and time in one table and update another table. 我试图在一个表中找到给定日期和时间的客户位置,并更新另一个表。 I have to generate letters (PDFs) with the right address at the time of the letter. 我必须在发出信件时用正确的地址生成信件(PDF)。 The customer has a movement record in a table that tells where he was on a given date and time. 客户在表中有一个移动记录,该记录可以告诉他在给定日期和时间的位置。 For example, we generate a letter to customer AAA with a date of 05/03/2016. 例如,我们生成给客户AAA的日期为2016年5月3日的信函。 It should have an address location of LocA. 它应该具有LocA的地址位置。 Customer BBB gets a letter with a date 05/22/2016 and it should have a location of LocE. 客户BBB收到一封日期为05/22/2016的信件,并且该信件的位置应为LocE。 I need to update CustomerLetter.AddressLocation with the value from CustomerMovement.AddressLocation , and I can't seem to get started on this. 我需要更新CustomerLetter.AddressLocation从价值CustomerMovement.AddressLocation ,我似乎无法得到这个开始。

Here are my tables. 这是我的桌子。

CREATE global temporary TABLE CustomerLetter (CustomerID varchar2(6), AddressLocation varchar2(6), LetterDate date);
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('AAA', NULL, '2016-05-03');
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('AAA', NULL, '2016-05-05');
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('AAA', NULL, '2016-08-14');
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('BBB', NULL, '2016-05-02');
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('BBB', NULL, '2016-05-29');
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('BBB', NULL, '2016-07-22');
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('CCC', NULL, '2016-03-06');
INSERT INTO CustomerLetter (CustomerID, AddressLocation, LetterDate) VALUES ('CCC', NULL, '2016-11-25');
commit;
CREATE global temporary TABLE CustomerMovement (CustomerID varchar2(6), ActionDate date, AddressLocation varchar2(6));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('AAA', 'locA', TO_DATE('2016-05-02 09:05:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('AAA', 'locA', TO_DATE('2016-05-04 14:05:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('AAA', 'locB', TO_DATE('2016-05-04 22:00:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('AAA', 'locE', TO_DATE('2016-07-02 20:00:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('AAA', 'locA', TO_DATE('2016-06-03 06:10:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('BBB', 'locE', TO_DATE('2016-05-10 03:00:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('BBB', 'locF', TO_DATE('2016-06-01 03:00:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('CCC', 'locA', TO_DATE('2016-10-10 03:00:00', 'yyyy-mm-dd HH24:MI:SS'));
INSERT INTO CustomerMovement (CustomerID, AddressLocation, ActionDate) VALUES ('CCC', 'locA', TO_DATE('2016-12-03 03:00:00', 'yyyy-mm-dd HH24:MI:SS'));
commit;

Use a subquery in your UPDATE statement to query the appropriate addressLocation value for each letter. UPDATE语句中使用子查询来查询每个字母的适当addressLocation值。

Using the tables and data you posted in your question, that would be something like this: 使用您在问题中发布的表格和数据,结果将如下所示:

update CustomerLetter cl
SET addresslocation = ( SELECT cm.addressLocation  
                        FROM   CutomerMovement cm
                        WHERE  cm.customerID = cl.customerID
                        AND    cm.actionDate <= cl.letterDate
                        ORDER BY cm.actionDate DESC
                        FETCH FIRST 1 ROW ONLY );

For each letter, find the customer movements that happened prior to the letterDate . 对于每个字母,找到在letterDate 之前发生的客户移动。 Then, use the addressLocation from most recent of those movements. 然后,使用最近移动中的addressLocation

If a letter has no customer movement records prior to the letter date, the address location for that letter will be null . 如果信函在信函日期之前没有客户移动记录,则该信函的地址位置将为null

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

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