我有一张表(A)和另一张表(B),其中包含以下信息:
我要做的是在图像中获得类似最终数据的内容........ 如果Receipt值在同一天与ExecutionDate + ExecutionTime不匹配...查询应该采用第一个下一个ExecutionDate + ExecutionTime
让我向您解释一下我想与2家零售商达成的目标:
COSTWH - 2017-12-26 11:15:09:000
这个应该取值
SellThru Refresh - 2017-12-26 23:30:00在表B
因为收货日期在执行时间之前(11:15:09:000 vs 23:30:00)所以最终数据应该是
COSTWH - 2017-12-26 11:15:09:000 - 2017-12-26 23:30:00
对于AMAZONCA - 2017-12-26 23:59:08:000 -----
表B中的值应为
SellThru Refresh - 2017-12-27- 04:30:00
为什么? ---因为收到日期是在当天最后一次执行之后('2017-12-26 23:59:08:000'VS'2017-12-26 23:30:00')所以查询应该采取下一个最接近的执行,在这种情况下是'2017-12-27','04:30:00'
所以AMAZONCA的数据看起来应该是
AMAZONCA - 2017-12-26 23:59:08:000 - 2017-12-27 04:30:00'
的sampleData:
表A
CREATE TABLE TableA(
Retailer VARCHAR(24) NOT NULL
,ReceiptDate DATETIME NOT NULL
);
INSERT INTO TableA VALUES ('AMAZON','2017-12-26 07:00:12:000');
INSERT INTO TableA VALUES ('MEIJER','2017-12-26 07:00:13:000');
INSERT INTO TableA VALUES ('SAMC01','2017-12-26 07:00:13:000');
INSERT INTO TableA VALUES ('SAMC01:COM','2017-12-26 07:00:13:000');
INSERT INTO TableA VALUES ('SAMSPR','2017-12-26 07:00:13:000');
INSERT INTO TableA VALUES ('WMT:COMCA','2017-12-26 07:00:14:000');
INSERT INTO TableA VALUES ('WMT001:COM','2017-12-26 07:00:14:000');
INSERT INTO TableA VALUES ('WMTCAN','2017-12-26 07:00:14:000');
INSERT INTO TableA VALUES ('TARGET','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARGET','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARGET','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARGET','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARGET','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARGET','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARGET','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARGET','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARGET','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARSTO','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARSTO','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARSTO','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARSTO','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARSTO','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARSTO','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARSTO','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARSTO','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('TARSTO','2017-12-26 09:30:11:000');
INSERT INTO TableA VALUES ('COSTCM','2017-12-26 11:15:09:000');
INSERT INTO TableA VALUES ('COSTCN','2017-12-26 11:15:09:000');
INSERT INTO TableA VALUES ('COSTCN','2017-12-26 11:15:09:000');
INSERT INTO TableA VALUES ('COSTCN:COM','2017-12-26 11:15:09:000');
INSERT INTO TableA VALUES ('COSTWH','2017-12-26 11:15:09:000');
INSERT INTO TableA VALUES ('AMAZONCA','2017-12-26 23:59:08:000');
INSERT INTO TableA VALUES ('AMAZON','2017-12-26 23:59:08:000');
INSERT INTO TableA VALUES ('SAMSMX','2017-12-26 23:59:08:000');
INSERT INTO TableA VALUES ('WMT001:COM','2017-12-26 23:59:08:000');
INSERT INTO TableA VALUES ('BBYCAN','2017-12-27 07:00:08:000');
INSERT INTO TableA VALUES ('MEIJER','2017-12-27 07:00:08:000');
INSERT INTO TableA VALUES ('SYNNEX','2017-12-27 07:00:08:000');
INSERT INTO TableA VALUES ('TECD01','2017-12-27 07:00:08:000');
INSERT INTO TableA VALUES ('SAMC01','2017-12-27 07:00:08:000');
INSERT INTO TableA VALUES ('SAMC01:COM','2017-12-27 07:00:08:000');
INSERT INTO TableA VALUES ('SAMSPR','2017-12-27 07:00:08:000');
INSERT INTO TableA VALUES ('WMT:COMCA','2017-12-27 07:00:08:000');
INSERT INTO TableA VALUES ('WMT001','2017-12-27 07:00:08:000');
INSERT INTO TableA VALUES ('WMTCAN','2017-12-27 07:00:08:000');
INSERT INTO TableA VALUES ('WMT001','2017-12-27 10:15:08:000');
INSERT INTO TableA VALUES ('COSTCM','2017-12-27 11:15:08:000');
INSERT INTO TableA VALUES ('COSTCN','2017-12-27 11:15:08:000');
INSERT INTO TableA VALUES ('COSTCN:COM','2017-12-27 11:15:08:000');
INSERT INTO TableA VALUES ('COSTWH','2017-12-27 11:15:08:000');
INSERT INTO TableA VALUES ('SAMSMX','2017-12-27 23:59:10:000');
表B
CREATE TABLE TableB(
jobname VARCHAR(30) NOT NULL
,ExecutionDate Date NOT NULL
,StartTime Time NOT NULL
,EndTime Time NOT NULL);
INSERT INTO TableB VALUES ('SellThru Refresh','2017-12-26','23:30:00','23:33:30');
INSERT INTO TableB VALUES ('SellThru Refresh','2017-12-27','04:30:00','04:48:29');
INSERT INTO TableB VALUES ('SellThru Refresh','2017-12-27','07:15:00','07:19:29');
INSERT INTO TableB VALUES ('SellThru Refresh','2017-12-27','11:30:00','11:36:15');
INSERT INTO TableB VALUES ('SellThru Refresh','2017-12-27','15:00:00','15:08:50');
INSERT INTO TableB VALUES ('SellThru Refresh','2017-12-27','23:30:00','23:34:52');
INSERT INTO TableB VALUES ('SellThru Refresh','2017-12-28','04:30:00','05:05:02');
INSERT INTO TableB VALUES ('SellThru Refresh','2017-12-28','07:15:00','07:27:38');
INSERT INTO TableB VALUES ('SellThru Refresh','2017-12-28','11:30:00','11:37:51');
INSERT INTO TableB VALUES ('SellThru Refresh','2017-12-28','15:00:00','15:13:38');
INSERT INTO TableB VALUES ('SellThru Refresh','2017-12-28','23:30:00','23:33:16');
答案 0 :(得分:2)
那应该是这样的:
select distinct Retailer
, ReceiptDate
, (select min(CAST(ExecutionDate AS DATETIME) + CAST(StartTime AS DATETIME)) -- first appearance
from TableB
where CAST(ExecutionDate AS DATETIME) + CAST(StartTime AS DATETIME) >= TableA.ReceiptDate) DataRefresh
from TableA
order by Retailer
, ReceiptDate
结果是这样的:
+------------+-------------------------+-------------------------+
| Retailer | ReceiptDate | DataRefresh |
+------------+-------------------------+-------------------------+
| AMAZON | 2017-12-26 07:00:12.000 | 2017-12-26 23:30:00.000 |
| AMAZON | 2017-12-26 23:59:08.000 | 2017-12-27 04:30:00.000 |
| AMAZONCA | 2017-12-26 23:59:08.000 | 2017-12-27 04:30:00.000 |
| BBYCAN | 2017-12-27 07:00:08.000 | 2017-12-27 07:15:00.000 |
| COSTCM | 2017-12-26 11:15:09.000 | 2017-12-26 23:30:00.000 |
| COSTCM | 2017-12-27 11:15:08.000 | 2017-12-27 11:30:00.000 |
| COSTCN | 2017-12-26 11:15:09.000 | 2017-12-26 23:30:00.000 |
| COSTCN | 2017-12-27 11:15:08.000 | 2017-12-27 11:30:00.000 |
| COSTCN:COM | 2017-12-26 11:15:09.000 | 2017-12-26 23:30:00.000 |
| COSTCN:COM | 2017-12-27 11:15:08.000 | 2017-12-27 11:30:00.000 |
| COSTWH | 2017-12-26 11:15:09.000 | 2017-12-26 23:30:00.000 |
| COSTWH | 2017-12-27 11:15:08.000 | 2017-12-27 11:30:00.000 |
| MEIJER | 2017-12-26 07:00:13.000 | 2017-12-26 23:30:00.000 |
| MEIJER | 2017-12-27 07:00:08.000 | 2017-12-27 07:15:00.000 |
| SAMC01 | 2017-12-26 07:00:13.000 | 2017-12-26 23:30:00.000 |
| SAMC01 | 2017-12-27 07:00:08.000 | 2017-12-27 07:15:00.000 |
| SAMC01:COM | 2017-12-26 07:00:13.000 | 2017-12-26 23:30:00.000 |
| SAMC01:COM | 2017-12-27 07:00:08.000 | 2017-12-27 07:15:00.000 |
| SAMSMX | 2017-12-26 23:59:08.000 | 2017-12-27 04:30:00.000 |
| SAMSMX | 2017-12-27 23:59:10.000 | 2017-12-28 04:30:00.000 |
| SAMSPR | 2017-12-26 07:00:13.000 | 2017-12-26 23:30:00.000 |
| SAMSPR | 2017-12-27 07:00:08.000 | 2017-12-27 07:15:00.000 |
| SYNNEX | 2017-12-27 07:00:08.000 | 2017-12-27 07:15:00.000 |
| TARGET | 2017-12-26 09:30:11.000 | 2017-12-26 23:30:00.000 |
| TARSTO | 2017-12-26 09:30:11.000 | 2017-12-26 23:30:00.000 |
| TECD01 | 2017-12-27 07:00:08.000 | 2017-12-27 07:15:00.000 |
| WMT:COMCA | 2017-12-26 07:00:14.000 | 2017-12-26 23:30:00.000 |
| WMT:COMCA | 2017-12-27 07:00:08.000 | 2017-12-27 07:15:00.000 |
| WMT001 | 2017-12-27 07:00:08.000 | 2017-12-27 07:15:00.000 |
| WMT001 | 2017-12-27 10:15:08.000 | 2017-12-27 11:30:00.000 |
| WMT001:COM | 2017-12-26 07:00:14.000 | 2017-12-26 23:30:00.000 |
| WMT001:COM | 2017-12-26 23:59:08.000 | 2017-12-27 04:30:00.000 |
| WMTCAN | 2017-12-26 07:00:14.000 | 2017-12-26 23:30:00.000 |
| WMTCAN | 2017-12-27 07:00:08.000 | 2017-12-27 07:15:00.000 |
+------------+-------------------------+-------------------------+