Need Help in Lag Function - MYSQL

Discussion in 'MySQL' started by selvakumar, Jul 6, 2021.

  1. selvakumar

    selvakumar New Member

    Joined:
    Jul 6, 2021
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    1
    Gender:
    Male
    Hey Folks

    Could you please help me to resolve my UAT issue. I need to do some calculation in my configs values (on 5 Mins Interval). For that i need to find delta between 2 times using LAG function.

    Sample table structure
    Code:
    create table T1 (Device varchar(25), ID integer, Interface varchar(100), Time DATETIME , Config integer);
    
    insert into T1 (Device, ID, Interface, Time, Config ) values('rddbernsg23oop42',32583,'rdd_test','2021-07-02 2:30:02',12552774);
    insert into T1 (Device, ID, Interface, Time, Config ) values('rddbernsg23oop42',32583,'rdd_test','2021-07-02 2:35:04',12587612);
    insert into T1 (Device, ID, Interface, Time, Config ) values('rddbernsg23oop42',32583,'rdd_test','2021-07-02 2:40:01',12640452);
    insert into T1 (Device, ID, Interface, Time, Config ) values('rddbernsg23oop42',32583,'rdd_test','2021-07-02 2:45:02',12901869);
    insert into T1 (Device, ID, Interface, Time, Config ) values('rddbernsg23oop42',32583,'rdd_test','2021-07-02 2:50:00',13374174);
    insert into T1 (Device, ID, Interface, Time, Config ) values('rddbernsg23oop42',32583,'rdd_test','2021-07-02 2:55:03',1600005);
    insert into T1 (Device, ID, Interface, Time, Config ) values('rddbernsg23oop42',32583,'rdd_test','2021-07-02 3:00:02',1601825);
    insert into T1 (Device, ID, Interface, Time, Config ) values('rddbernsg23oop42',32583,'rdd_test','2021-07-02 3:05:05',1604848);
    
    I have below query to get previous day config values

    Code:
    SELECT Device , ID , Interface, Time,  Config , lag(Config ,1) over (order by Time) as "Lag time",
    Config - lag(Config,1) OVER( order by Time) as 'Delta config'
    FROM T1
    WHERE Time BETWEEN FROM_UNIXTIME(1625192700) AND FROM_UNIXTIME(1625195400);
    
    It works fine in online Mysql portal. But in my UAT portal it fails.

    is that any thing related to Version Problem?

    In UAT


    In UAT It works only when the config have values in Ascending order it means if the table T1 have only first 5 rows. ( time between 2;30 to 2.50)
    Code:
    SELECT Device, ID , Interface, Time,  Config , lag(Config ,1) over (order by Time) as "Lag time" ,
    Config-lag(Config,1) OVER( order by Time)) as 'Delta config',
    FROM T1
    WHERE Time BETWEEN FROM_UNIXTIME(1625192700) AND FROM_UNIXTIME(1625194200);
    
    working fine. But

    It fails when i include 6th row onwards by adjusting time in where clause.
    Code:
    SELECT Device , ID , Interface, Time,  Config ,
    lag(Config ,1) over (order by Time) as "Lag time" ,
    Config-lag(Config,1) OVER( order by Time)) as 'Delta config'
    FROM T1
    WHERE Time BETWEEN FROM_UNIXTIME(1625192700) AND FROM_UNIXTIME(1625195400);
    
    Please help.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice