Alternate of lead lag function in SQL Server 2008

后端 未结 3 618
孤城傲影
孤城傲影 2020-12-01 21:37

I want to compare the current row with a value in the next row. SQL has LEAD and LAG functions to get the next and previous values but I can not us

相关标签:
3条回答
  • 2020-12-01 22:05

    Well, I would suggest a very simple solution if you do not have a sequential row id but a different step (if some records were deleted for example..):

    declare @t table(id int, obj_name varchar(5))
    
    insert @t select 1,'a'
    
    insert @t select 5,'b'
    
    insert @t select 22,'c'
    
    insert @t select 543,'d'
    
    ---------------------------------
    select *from @t
    

    Example Source Table @t:

    ---------------------------------
    id  obj_name
    
    1   a
    
    5   b
    
    22  c
    
    543 d
    
    ---------------------------------
    

    Select with self join

    select obj_name_prev=tt.obj_name, obj_name_next=min(t.obj_name)
    
    from @t t
    
    join @t tt on tt.id < t.id
    
    group by tt.obj_name
    

    Result:

    ---------------------------------
    obj_name_prev   obj_name_next
    
    a   b
    
    b   c
    
    c   d
    
    ---------------------------------
    
    0 讨论(0)
  • 2020-12-01 22:22

    Just posting a more complex join using two different tables created with Gordon's foundation. Excuse the specific object names, but you'll get the gist. Gets the percentage change in samples from one to the next.

    SELECT 
          fm0.SAMPLE curFMSample
        , fm1.SAMPLE nextFMSample
        , fm0.TEMPERATURE curFMTemp
        , fm1.TEMPERATURE nextFMTemp
        , ABS(CAST((fm0.Temperature - fm1.Temperature) AS DECIMAL(4, 0)) / CAST(fm0.TEMPERATURE AS DECIMAL(4, 0))) AS fmTempChange
        , fm0.GAUGE curFMGauge
        , fm1.GAUGE nextFMGauge
        , ABS(CAST((fm0.GAUGE - fm1.GAUGE) AS DECIMAL(4, 4)) / CAST(fm0.GAUGE AS DECIMAL(4, 4))) AS fmGaugeChange
        , fm0.WIDTH curFMWidth
        , fm1.WIDTH nextFMWidth
        , ABS(CAST((fm0.Width - fm1.Width) AS DECIMAL(4, 2)) / CAST(fm0.Width AS DECIMAL(4, 2))) AS fmWidthChange
        , cl0.TEMPERATURE curClrTemp
        , cl1.TEMPERATURE nextClrTemp
        , ABS(CAST((cl0.Temperature - cl1.Temperature) AS DECIMAL(4, 0)) / CAST(cl0.TEMPERATURE AS DECIMAL(4, 0))) AS clrTempChange
    FROM 
        dbo.COIL_FINISHING_MILL_EXIT_STR02 fm0
        INNER JOIN dbo.COIL_FINISHING_MILL_EXIT_STR02 fm1 ON (fm0.SAMPLE = fm1.SAMPLE - 1 AND fm1.coil = fm0.coil)
        INNER JOIN dbo.COIL_COILER_STR02 cl0 ON fm0.coil = cl0.coil AND fm0.SAMPLE = cl0.SAMPLE
        INNER JOIN dbo.COIL_COILER_STR02 cl1 ON (cl0.SAMPLE = cl1.SAMPLE - 1 AND cl1.coil = cl0.coil)
    WHERE
        fm0.coil = 2015515872
    0 讨论(0)
  • 2020-12-01 22:24

    In your case, the ids appear to be numeric, you can just do a self-join:

    select t.*
    from table t join
         table tnext
         on t.id = tnext.id - 1 and
            t.StatusId = 1 and
            tnext.StatusId = 6 and
            datediff(second, t.MinStartTime, tnext.MinStartTime) < 60;
    

    This isn't quite the same minute. It is within 60 seconds. Do you actually need the same calendar time minute? If so, you can do:

    select t.*
    from table t join
         table tnext
         on t.id = tnext.id - 1 and
            t.StatusId = 1 and
            tnext.StatusId = 6 and
            datediff(second, t.MinStartTime, tnext.MinStartTime) < 60 and
            datepart(minute, t.MinStartTime) = datepart(minute, tnext.MinStartTime);
    
    0 讨论(0)
提交回复
热议问题