gpt4 book ai didi

sql - 取下一个日期值 SQL Server

转载 作者:行者123 更新时间:2023-12-02 01:05:10 26 4
gpt4 key购买 nike

我有一张表 (A) 和另一张表 (B),其中包含以下信息:

enter image description here

我想做的是在图像中得到类似FINAL DATA 的东西......如果 Receipt 值与同一天的 ExecutionDate+ExecutionTime 不匹配...查询应该取第一个 next ExecutionDate+ExecutionTime

让我向您解释一下我想从 2 家零售商那里得到什么:

COSTWH - 2017-12-26 11:15:09:000

这个应该取值

SellThru Refresh - 表B中2017-12-26 23:30:00

因为接收日期早于执行时间(11:15:09:000 对 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 刷新 - 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 的数据应该是这样的

亚马逊 - 2017-12-26 23:59:08:000 - 2017-12-27 04:30:00'


样本数据:

表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');

最佳答案

那么应该是这样的:

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 |
+------------+-------------------------+-------------------------+

关于sql - 取下一个日期值 SQL Server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48118970/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com