将Excel转换为SQL

时间:2018-01-23 14:18:07

标签: sql excel

Hi All以下是我的查询结果,其中添加了列以查找2个事件之间的时间。到目前为止,我还提供了我的错误代码!

SELECT
PAN.[FULL PARCEL ID], 
PAN.[12 DIGIT PARCEL ID], 
PAN.[NO OF PARCELS], 
PAN.[SERVICE ID], 
PAN.[PAN PROCESS DATE TIME],
PAN.[Late Time Measure DD:HH:MM], 
PAN.[1ST PHYSICAL SCAN], 
PAN.[HUB SCAN], 
PAN.[SORT LOCATION], 
PAN.[PAN STATUS], 
PAN.[PAN FUNCTION], 
RLZ.ReportGroup,
ORD.PicksetNo,
ORD.PicksetPrintTS,
TASK.TaskStart,
TASK.TaskEnd,
ORD.PicksetConfirmTS,
ORD.EarliestPickDate
FROM CHDS_Sandbox.dbo.PANTEST PAN
LEFT JOIN CHDS_Common.dbo.OMOrder ORD ON ORD.AddressBarcode = PAN.[FULL PARCEL ID]
LEFT JOIN CHDS_Common.dbo.RackListZone RLZ ON RLZ.Loc = ORD.ProductWhsLocation
LEFT JOIN CHDS_Common.dbo.TaskScan TASK ON TASK.Pickset = ORD.PicksetNo

我需要找出当ORD.PicksetConfirmTS发生时和PAN。[1ST PHYSICAL SCAN]发生时的时差。我需要这个也显示负面时间。

我试过了:

CONCAT(
       DATEDIFF(DAY, PAN.[PAN PROCESS DATE TIME], ORD.PicksetConfirmTS),'-',
       DATEDIFF(HOUR, PAN.[PAN PROCESS DATE TIME], ORD.PicksetConfirmTS),':',
       DATEDIFF(MINUTE, PAN.[PAN PROCESS DATE TIME], ORD.PicksetConfirmTS),':',
       DATEDIFF(SECOND, PAN.[PAN PROCESS DATE TIME], ORD.PicksetConfirmTS))

但是我在-1--12-456-12345的情况下加入了消极的。

Excel很简单:

=IF(E2-N2<0,"-"&TEXT(ABS(E2-N2),"dd - hh:mm"),TEXT(ABS(E2-N2),"dd - hh:mm"))

FULL PARCEL ID  NO OF PARCELS   PAN PROCESS DATE TIME   Late Time Measure DD:HH:MM  1ST PHYSICAL SCAN   HUB SCAN    SORT LOCATION   PAN STATUS  ReportGroup PicksetNo   PicksetPrintTS  TaskStart   TaskEnd PicksetConfirmTS    EarliestPickDate
JJD2102210131951257 1   02/01/2018 21:40    00:00:11    02/01/2018 21:29    02/01/2018 21:29    Las Vasg    LATE    UGLY    181436  02/01/2018 15:13    (null)  (null)  02/01/2018 21:02    02/01/2018
JJD2102210131988232 1   07/01/2018 21:16    00:00:16    07/01/2018 21:00    07/01/2018 21:00    Italy   LATE    UGLY    183155  07/01/2018 16:22    (null)  (null)  07/01/2018 20:39    07/01/2018
JJD2102210131951259 1   02/01/2018 21:40    00:00:11    02/01/2018 21:29    02/01/2018 21:29    Las Vasg    LATE    UGLY    181436  02/01/2018 15:13    (null)  (null)  02/01/2018 21:02    02/01/2018
JJD2102210131951260 1   02/01/2018 21:40    00:00:17    02/01/2018 21:23    02/01/2018 21:23    Las Vasg    LATE    UGLY    181436  02/01/2018 15:13    (null)  (null)  02/01/2018 21:02    02/01/2018
JJD2102210131951261 1   02/01/2018 21:40    00:00:17    02/01/2018 21:23    02/01/2018 21:23    Las Vasg    LATE    UGLY    181436  02/01/2018 15:13    (null)  (null)  02/01/2018 21:02    02/01/2018
JJD2102210190421300 1   02/01/2018 21:39    00:00:51    02/01/2018 20:48    02/01/2018 20:48    Las Vasg    LATE    UGLY    181382  02/01/2018 12:47    (null)  (null)  02/01/2018 21:22    02/01/2018
JJD2102210190434368 1   07/01/2018 21:15    00:00:01    07/01/2018 21:14    07/01/2018 21:14    Italy   LATE    UGLY    183155  07/01/2018 16:22    (null)  (null)  07/01/2018 20:39    07/01/2018
JJD2102210190434367 1   07/01/2018 21:15    00:00:14    07/01/2018 21:01    07/01/2018 21:01    Italy   LATE    UGLY    183155  07/01/2018 16:22    (null)  (null)  07/01/2018 20:39    07/01/2018
JJD2102210190436947 1   09/01/2018 16:39    00:00:01    09/01/2018 16:38    09/01/2018 16:38    Italy   LATE    UGLY    183613  09/01/2018 06:22    (null)  (null)  09/01/2018 16:23    09/01/2018

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:1)

使用此答案的公式
https://stackoverflow.com/a/12380293/1132334

我们可以写

SELECT
    case SIGN(DATEDIFF(SECOND, ORD.PicksetConfirmTS, PAN.[PAN PROCESS DATE TIME])) WHEN 1 THEN '' ELSE '-' END+
    cast(ABS(DATEDIFF(SECOND, ORD.PicksetConfirmTS, PAN.[PAN PROCESS DATE TIME]))/86400 as varchar(50))+':'+
    convert(varchar,DATEADD(S,ABS(DATEDIFF(SECOND, ORD.PicksetConfirmTS, PAN.[PAN PROCESS DATE TIME])),0),108)

我们需要包括两次秒的计算这一事实有点不幸,但欠查询语义。

保留差异的符号,样本输出:

  

-1:23:41:40

原始尝试的问题是合乎逻辑的:不同的DATEDIFF调用将在所有四种情况下计算相同的差异,仅在不同的时间单位。需要的是一系列分歧。 convert(..., 108)函数可以在几秒到几小时内完成繁重的工作,只需要我们自己需要的时间,幻数86400就是一天中的秒数。