连续计算表中行之间的值

时间:2014-12-03 03:26:35

标签: sql-server tsql

我试图执行一个t-sql,它能够通过将连续行的datetime值减去前一个的datetime值来执行一些计算。

例如:

             Col1                            Col2
-------------------------------------------------------------------
row 1: | ENTRY_DOOR_CLOSE                | 2/12/2014 16:41:40:4140
row 2: | EXIT_DOOR_CLOSE_ENTRY_DOOR_OPEN | 3/12/2014 16:41:40:4140
row 3: | ENTRY_DOOR_CLOSE                | 4/12/2014 16:41:40:4140
row 4: | EXIT_DOOR_CLOSE_ENTRY_DOOR_OPEN | 5/12/2014 16:41:40:4140
--------------------------------------------------------------------

结果:

       Col1          Col2 
---------------------------------------------------------------------
Row 1: | Diff   | Row2.DateTime - Row1.DateTime
Row 2: | Diff   | Row4.DateTime - Row3.DateTime
---------------------------------------------------------------------

有人可以提出解决此问题的想法吗?

4 个答案:

答案 0 :(得分:2)

在SQL Server 2012+中,您可以使用lead()功能:

select 'Diff' as col1,
       datediff(second, col2, col2_next) as diff_in_seconds
from (select t.*, lead(col2) over (order by col2) as col2_next
      from table t
     ) t
where col1 = 'ENTRY_DOOR_CLOSE';

这假定值是交错的,如问题所示。

答案 1 :(得分:0)

如果我没有使用SQL 2k12,那么使用CTE可以解决我的问题

;WITH valuedTable AS (
    SELECT
        ROW_NUMBER() OVER (PARTITION BY ScxxID, SxxID ORDER BY RecordTime) AS RowID
        , ScxxID
        , SxxID
        , Exxx
        , RecordTime
        , ProcessName
    FROM
        database..xxx
    WHERE
        ProcessName = 'EXIT_DOOR_CLOSE_ENTRY_DOOR_OPEN'
        OR
            ProcessName = 'ENTRY_DOOR_CLOSE'
)
SELECT
    valuedTable.ProcessName
    , valuedTable.RecordTime
    , nex.ProcessName
    , nex.RecordTime
    , DATEDIFF(S, valuedTable.RecordTime, nex.RecordTime) DIFF
FROM
    valuedTable
INNER JOIN
    ( valuedTable nex ON nex.RowID = valuedTable.RowID + 1 )
    AND 
    ( nex.ProcessName = 'EXIT_DOOR_CLOSE_ENTRY_DOOR_OPEN' )

答案 2 :(得分:0)

如果你使用sql server 2012 - 使用这个(你的表是有序的,但是对于非有序的表,这个也是多变的):

;WITH CTE AS (SELECT ROW_NUMBER() OVER (ORDER BY Col2) AS RN, Col1, Col2 
             FROM YourTable)

    SELECT 'Diff' AS Col1, DATEDIFF(HOUR,a.Col2,x.Col2) AS Col2
    FROM CTE a
    CROSS APPLY (SELECT TOP 1 Col2 FROM CTE b WHERE Col1 = 'EXIT_DOOR_CLOSE_ENTRY_DOOR_OPEN' AND b.RN > a.RN ORDER BY Col2 ASC) x
    WHERE Col1 = 'ENTRY_DOOR_CLOSE'

答案 3 :(得分:0)

希望这会有所帮助

--CREATE A TEMPORARY TABLE TO HOLD THE GIVEN DATA
DECLARE @Table AS TABLE
(
  ID INT IDENTITY(1,1)
 ,Col1  VARCHAR(50)
 ,Col2 DATETIMEOFFSET(0)
)

INSERT INTO @Table (COl1,Col2) VALUES  ('ENTRY_DOOR_CLOSE', '2014-12-02'),
                                        ('EXIT_DOOR_CLOSE_ENTRY_DOOR_OPEN' , '2014-12-03')
                                        ,('ENTRY_DOOR_CLOSE','2014-12-04')
                                        ,('EXIT_DOOR_CLOSE_ENTRY_DOOR_OPEN' , '2014-12-05')

--Using common table expression do the following 
;WITH CTE AS
(
    SELECT

         ROW_NUMBER() OVER (ORDER BY ID) AS RowID
        ,CONVERT(date,Col2) AS DateColumn 

    FROM @Table

)

SELECT
 'DIF' AS Col1
,DATEDIFF(DD,SEcondCTE.DateColumn,FirstCTE.DateColumn)
FROM
CTE FirstCTE
INNER JOIN
CTE SEcondCTE
ON
FirstCTE.RowID = SEcondCTE.RowID + 1
WHERE FirstCTE.RowID % 2 =0