如何在sql-server中更正此scalaire函数?

时间:2019-05-25 10:04:23

标签: sql-server

我有这张桌子

CREATE TABLE BTInteractions
(
 [NumBt]  integer ,
 [NuORD] integer,
[EntryTime]      [DateTime] NOT NULL, 
[FinalTime] [DateTime] NOT NULL,
  [StopTime] float

);

INSERT INTO dbo.BtInteractions( NumBt,NuORD,EntryTime, FinalTime,StopTime  )
values
(120,1,'2016-05-02 13:30:00','2016-05-02 21:50:00',492)
,(120,2,'2016-05-02 14:45:00','2016-05-02 22:00:00',450)
,(120,3,'2016-05-02 16:30:00','2016-05-02 22:21:00',348)
,(120,0,'2016-05-02 13:30:00','2016-05-02 22:21:00',840)
;

我有相同的“ NU_NUMBT”,由不同的NU_ORD(1,2)组成 每个NU_ORD都有一个开始日期时间(DT_DEB)和一个最终日期时间(Dt_FIN)。 我想计算每个NU_ORD的(DT_DEB)和(Dt_FIN)之间的差异,然后将这两个差异的总和放入NU_ORD(0) 我有一个条件:即使是(Dt_FIN),也不能在两个连续的(DT_DEB)中出现时间重叠 如果有时间重叠,我不必考虑结果中的最小日期时间,所以我必须选择satetime的最小和最大值,并将它们的总和计算为chown:

SELECT * FROM BTInteractions order by  NU_ORD asc

这是我的代码,用于计算我引用的ZLK answer

连续行之间的差异
   WITH BtActions AS
(
    select ROW_NUMBER() OVER (ORDER BY [DT_DEB]) -- Create an index number ordered by time.
         AS [Sequence],
    * from BtInteractions
)
SELECT *,
       ISNULL(DATEDIFF(Minute, 
                      (SELECT other.DT_DEB 
                              FROM  BtActions Other 
                              WHERE other.Sequence = BtActions.Sequence - 1 ), 
                       BtActions.DT_DEB)/60.0, 
               0) 
    AS MinutesFromDEB 
    ,
       ISNULL(DATEDIFF(Minute, 
                      (SELECT other.DT_FIN 
                              FROM  BtActions Other 
                              WHERE other.Sequence = BtActions.Sequence - 1 ), 
                       BtActions.DT_DEB)/60.0, 
               0) 
    AS MinutesFromFIN
FROM BtActions ;

然后创建一个标尺函数(因为我有大量的NU_NUMBT)

这是我的函数,它的StopTime结果显示为绿色,但是每次都给我零,而我不知道问题所在

ALTER FUNCTION [dbo].[GetHeureREAL](@NU_NUMBT T_NUMBT)/*, @ID_DATDEB timestamp, @ID_DATFIN timestamp)*/
RETURNS float
AS
BEGIN
  DECLARE @debDATE datetime
  DECLARE @finDATE datetime
  DECLARE @DIFFDEB datetime
  DECLARE @DIFFIN datetime
  DECLARE @RESULTAT float
  DECLARE @NU_ORD int
  DECLARE @NUMBT int 
  DECLARE @I INT
  DECLARE @DurARR float
  DECLARE @HEUREA float

  DECLARE @compDEB float
  DECLARE @compFIN  float

  SET @debDATE = (select DT_DEB from BtInteractions where NU_NUMBT = @NU_NUMBT 
                        and NU_ORD = @NU_ORD)
  SET @finDATE = (select DT_FIN from BtInteractions where NU_NUMBT = @NU_NUMBT 
                        and NU_ORD = @NU_ORD)

  SET @RESULTAT = 0
  SET @I = 0

  While @debDATE <= @finDATE
  begin

     SET @DurARR = 0
     SET @HEUREA = 0
     SET @NUMBT = @NU_NUMBT

     /*EXTRACTION NUM ORDRE*/
       SET @NU_ORD  = (select NU_ORD FROM BtInteractions WHERE NU_ORD != 0  AND NU_NUMBT = @NU_NUMBT  ) 



    SET @DurARR = (select (DATEDIFF(MINUTE,DT_DEM,DT_FIN)/60.0) from BtInteractions where NU_NUMBT = @NU_NUMBT 
                        and NU_ORD = @NU_ORD)

    SET @HEUREA = (select (DATEDIFF(MINUTE,DT_DEB,DT_FIN)/60.0) from BtInteractions where NU_NUMBT = @NU_NUMBT 
                        and NU_ORD = @NU_ORD)


    /*comparaison date debut*/
   SET @compDEB =  (select MinutesFromDEB from BtInteractions where NU_NUMBT= @NU_NUMBT  )

     /*comparaison date fin*/ 

   SET @compFIN =  (select MinutesFromFIN from BtInteractions where NU_NUMBT= @NU_NUMBT  )


      IF ((@compDEB < 0 ) and ( @compFIN > 0))
      BEGIN 
      SET  @DurARR = ABS(@DurARR + @compDEB)
      SET  @HEUREA = ABS(@HEUREA + @compDEB)
      SET @NU_ORD = @NU_ORD+1 

      END

       IF ((@compDEB > 0 ) and ( @compFIN < 0))
      BEGIN 
      SET  @DurARR = ABS(@DurARR + @compFIN)
      SET  @HEUREA = ABS(@HEUREA + @compFIN)

      SET @NU_ORD = @NU_ORD+1 


      /*SET @RESULTAT = @RESULTAT + isnull(( @compFIN - @compDEB ),0)*/
      END
       IF ((@compDEB < 0 ) and ( @compFIN < 0))
      BEGIN 
      SET  @DurARR = @DurARR + @compFIN+@compDEB
      SET  @HEUREA = @HEUREA + @compFIN+@compDEB

      SET @NU_ORD = @NU_ORD+1 

      END

     END



     SET @RESULTAT = @RESULTAT+ @DurARR 


  RETURN IsNull(@RESULTAT,0)
END
GO

请问如何计算NU_ORD 0的stopTime?还是除了scalair函数之外,还有其他想法可以得到我喜欢的结果吗?

0 个答案:

没有答案