0

我一直在努力解决这个问题,但我不太擅长 tsql。

这就是我得到的,我不能让 DateTime 计算得很好。我得到的是 A 和 B 之间的总和,但不是总和。例如,在最后一列中,我有一个 0,它让我回到 -x。

下面是过程,部分数据是这样的:

Code_Procedure  date_evenement                 codes_situation        
---------------------------------------------------------------
000079500000    2013-05-21 13:07:00.000        COMCFM              
000079500000    2013-05-21 20:24:00.000        PCHCFM     
000079500000    2013-05-22 09:58:00.000        PCHCFM                           
000079500000    2013-05-23 00:00:00.000        AARCFM                         
000079500000    2013-05-23 00:00:00.000        LIVCFM                 
000079600000    2013-05-21 13:07:00.000        COMCFM               
000079600000    2013-05-21 20:24:00.000        PCHCFM              
000079600000    2013-05-22 11:18:00.000        PCHCFM               
000079600000    2013-05-23 00:00:00.000        AARCFM                
000079600000    2013-05-23 00:00:00.000        LIVCFM              

这是过程:

DECLARE @COMCFM TABLE(numero_colis VARCHAR(25), date_evenement DATETIME);
INSERT @COMCFM SELECT TOP(5) numero_colis, date_evenement FROM cartitem_colis_postaux_etats WHERE (code_situation = 'PCH' AND code_justification = 'CFM')

WHILE (SELECT COUNT(*) FROM @COMCFM) > 0
BEGIN   
    DECLARE @Colis TABLE(numero_colis VARCHAR(25), date_evenement DATETIME, code_situation_code_justification NVARCHAR(32));
    INSERT @Colis SELECT numero_colis, date_evenement, code_situation + code_justification FROM cartitem_colis_postaux_etats WHERE numero_colis = (SELECT TOP(1) numero_colis FROM @COMCFM) ORDER BY numero_colis, date_evenement

    ;WITH CTE AS
    (
        Select DISTINCT
        *
        ,ROW_NUMBER() OVER(PARTITION BY numero_colis ORDER BY date_evenement ASC) Rn FROM @Colis
    ),CTE1 AS 
    (
        SELECT DISTINCT 
            A.*
            ,DATEDIFF(mi, B.date_evenement, A.date_evenement) AS DIFF
        FROM CTE A INNER JOIN CTE B On B.Rn + 1 = A.Rn
        UNION All
        SELECT A.*, 0 FROM CTE A Where Rn = 1
    )

    SELECT 
        A.*
        ,ISNULL((
            SELECT
                A.DIFF + B.DIFF
            FROM CTE1 AS B
            WHERE A.numero_colis = B.numero_colis
            AND A.Rn = B.Rn + 1), 0) AS Sums
    FROM CTE1 AS a
    ORDER BY numero_colis, Rn ASC

    DELETE FROM @Colis
    DELETE FROM @COMCFM WHERE numero_colis = (SELECT TOP(1) numero_colis FROM @COMCFM)
END
4

1 回答 1

0

我不确定你想要达到什么目标。您是否需要日期差异作为累积值?如果需要,请使用此代码更改存储过程的 BEGIN-END 块

BEGIN   
    DECLARE @Colis TABLE(numero_colis VARCHAR(25), date_evenement DATETIME, code_situation_code_justification NVARCHAR(32));
    INSERT @Colis SELECT numero_colis, date_evenement, code_situation + code_justification FROM cartitem_colis_postaux_etats WHERE numero_colis = (SELECT TOP(1) numero_colis FROM @COMCFM) ORDER BY numero_colis, date_evenement

    ;WITH CTE AS
    (
        SELECT DISTINCT
            *,
            ROW_NUMBER() OVER(PARTITION BY numero_colis ORDER BY date_evenement ASC) Rn 
        FROM @Colis
    ),CTE1 AS 
    (
        SELECT A.*, 0 AS CummulatedDiff
        FROM CTE A 
        WHERE Rn = 1

        UNION ALL

        SELECT DISTINCT A.*, B.CummulatedDiff + DATEDIFF(mi, B.date_evenement, A.date_evenement) AS CummulatedDiff
        FROM CTE AS A INNER JOIN 
             CTE1 AS B ON B.Rn + 1 = A.Rn AND B.numero_colis = A.numero_colis
    )

    SELECT *
    FROM CTE1 AS a
    ORDER BY numero_colis, Rn ASC

    DELETE FROM @Colis
    DELETE FROM @COMCFM WHERE numero_colis = (SELECT TOP(1) numero_colis FROM @COMCFM)
END

我希望这能让你更进一步地实现你的目标。

于 2013-06-20T08:58:53.553 回答