我正在尝试在一个表中查找给定日期和时间的客户位置,并更新另一个表。
我必须在发出信件时用正确的地址生成信件(PDF)。
客户在表中有一个移动记录,该记录可以告诉他在给定日期和时间的位置。
例如,我们生成给客户AAA的日期为2016年5月3日的信函。它应该具有LocA的地址位置。
客户BBB收到一封日期为05/22/2016的信件,并且该信件的位置应为LocE。
我需要使用CustomerLetter.AddressLocation
中的值更新CustomerMovement.AddressLocation
,但似乎无法对此进行入门。
这是我的桌子。
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;
答案 0 :(得分:2)
在您的UPDATE
语句中使用子查询来为每个字母查询适当的addressLocation
值。
使用您在问题中发布的表格和数据,就像这样:
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 );
对于每个字母,找到在letterDate
之前 发生的客户移动。然后,使用最近一次移动中的addressLocation
。
如果一封信件在该信件的日期之前没有客户移动记录,则该信件的地址将为null
。