Home Translate Lead/Lag function to MySQL
Reply: 0

Translate Lead/Lag function to MySQL

user962
1#
user962 Published in April 22, 2018, 6:00 pm

How do I translated the below codes so that it runs in MySQL. I am new to MySQL and finding it difficult to understand few things.

CODE 1:

SELECT t1.user_id,
           t1.visit_month LEAD (t1.visit_month, 1) OVER (partition BY t1.user_id ORDER BY t1.user_id, t1.visit_month)
    FROM (SELECT 
    user_id,
    month(date_time) as visit_month
FROM
    tbl_activity
    group by 1, 2
ORDER BY 1 , 2) t1;

Desired Output for Code 1

CODE 2:

SELECT user_id,
       visit_month,
       lead,
       lead — visit_month AS time_diff
FROM table3

CODE 3:

SELECT user_id,
       Visit_month,
       lag(visit_month, 1) over (partition BY user_id ORDER BY user_id, visit_month)
 FROM table

CODE 4:

SELECT user_id,
       visit_month,
       lag,
       visit_month — lag AS time_diff
 FROM table2
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.308957 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO