DATEDIFF总是在最后一次交互中向我发送Null结果

时间:2017-07-09 01:59:41

标签: sql sql-server-2012 iif date-difference

我真的需要你的帮助以下查询。我有以下查询:

var

,结果是

SELECT 
Country,
CRM_OBJ_ID as "Service_Order_ID",
"CRM_NUMINT" as "Service_Order_Line",
nullif(CRM_CRD_AT,'') as "Service_Order_Creation_Date",
--nullif(ZDAT_PO,'') as "Part_Order_Release_Date",

NULLIF(ERDAT,'') as "Item_Creation_Date",
NULLIF("SHPFR_DATE",'') as "Shipped_Date",
NULLIF("ACT_DATE",'') as "Delivered_Date",

DATEDIFF(day,iif(max("ACT_DATE") over (PARTITION BY CRM_OBJ_ID)='',
                    IIF(max("SHPFR_DATE") over(PARTITION BY CRM_OBJ_ID)='',
                        IIF(max(ERDAT) over(PARTITION BY CRM_OBJ_ID) is null,max("CRM_CRD_AT") over(PARTITION BY CRM_OBJ_ID),max(ERDAT) over(PARTITION BY CRM_OBJ_ID)),                             
                    max("SHPFR_DATE") over(PARTITION BY CRM_OBJ_ID)),
                max("ACT_DATE") over (PARTITION BY CRM_OBJ_ID)),
        GETDATE()) as dif

FROM ZOH_SVS43_IDL

LEFT JOIN ZO_SDA05_IDL ON ZOH_SVS43_IDL.CRM_OBJ_ID=ZO_SDA05_IDL.VBELN and ZOH_SVS43_IDL.CRM_NUMINT=ZO_SDA05_IDL.POSNR

where CRM_OBJ_ID in ('7008097791','7007987492','7008002892','7008097849')

Group by Country,CRM_OBJ_ID,CRM_NUMINT,CRM_CRD_AT,ZDAT_OCMP,ERDAT,SHPFR_DATE,ACT_DATE,CSM_CLDA,ZDAT_PO

Order by Country,CRM_OBJ_ID,CRM_NUMINT

我想要做的是计算btw日期的差异,为最后状态 - 今天。

逻辑是,如果Delivered为null,则查找Shipped,如果为null,则查找Item创建日期,如果此字段为null,则查找创建日期。

正确的结果必须是

Country service_order   Line    Create_Date Item_Creation_Date  Shipped_Date    Delivered_Date  Today() Dif
CL  7007987492  10  5/2/2017    NULL    NULL    NULL    7/8/2017    65
CL  7007987492  20  5/2/2017    5/2/2017    5/3/2017    5/5/2017    7/8/2017    65
MX  7008002892  0   5/11/2017   NULL    NULL    NULL    7/8/2017    41
MX  7008002892  20  5/11/2017   5/29/2017   NULL    NULL    7/8/2017    41
MX  7008097791  10  7/4/2017    7/4/2017    NULL    NULL    7/8/2017    4
MX  7008097791  20  7/4/2017    7/4/2017    7/5/2017    NULL    7/8/2017    4
MX  7008097849  10  7/4/2017    NULL    NULL    NULL    7/8/2017    NULL

任何指导或回答都会非常感谢,谢谢你们。

1 个答案:

答案 0 :(得分:0)

根据您所陈述的逻辑,在日期列使用的优先顺序上获取日期差异,您可以使用COALESCE函数获取第一个非空值。

使用“正确”的结果集,我将这些值插入到临时表中,以快速显示COALESCE功能如何与DATEDIFF一起使用。这是示例脚本:

IF OBJECT_ID('tempdb..#Orders') IS NOT NULL
   BEGIN
      DROP TABLE #ORders;
   END;

CREATE TABLE #Orders
   (
      Country            VARCHAR(2)
    , service_order      BIGINT
    , Line               INT
    , Create_Date        DATE
    , Item_Creation_Date DATE
    , Shipped_Date       DATE
    , Delivered_Date     DATE
    , Today              DATE
   );

INSERT INTO #Orders (
                       Country
                     , service_order
                     , Line
                     , Create_Date
                     , Item_Creation_Date
                     , Shipped_Date
                     , Delivered_Date
                     , Today
                    )
VALUES ('CL', 7007987492, 10, '5/2/2017', NULL, NULL, NULL, '7/8/2017')
     , ('CL', 7007987492, 20, '5/2/2017', '5/2/2017', '5/3/2017', '5/5/2017', '7/8/2017')
     , ('MX', 7008002892, 0, '5/11/2017', NULL, NULL, NULL, '7/8/2017')
     , ('MX', 7008002892, 20, '5/11/2017', '5/29/2017', NULL, NULL, '7/8/2017')
     , ('MX', 7008097791, 10, '7/4/2017', '7/4/2017', NULL, NULL, '7/8/2017')
     , ('MX', 7008097791, 20, '7/4/2017', '7/4/2017', '7/5/2017', NULL, '7/8/2017')
     , ('MX', 7008097849, 10, '7/4/2017', NULL, NULL, NULL, '7/8/2017');

SELECT O.Country
     , O.service_order
     , O.Line
     , O.Create_Date
     , O.Item_Creation_Date
     , O.Shipped_Date
     , O.Delivered_Date
     , O.Today
     , FirsNonNullDate = COALESCE(O.Delivered_Date, O.Shipped_Date, O.Item_Creation_Date, O.Create_Date)
     , DateDiffInDays  = DATEDIFF(DAY, COALESCE(O.Delivered_Date, O.Shipped_Date, O.Item_Creation_Date, O.Create_Date), Today)
FROM   #Orders AS O;

这导致以下屏幕截图中显示的内容。希望这会对你有所帮助。

enter image description here