VintageGold / Data690_MathML

A place to ask questions and collaborate with peers
MIT License
1 stars 2 forks source link

LAG() and LEAD() in SQL #2

Closed xinxueHEAI closed 3 years ago

xinxueHEAI commented 3 years ago

@Colsai Could you explain how LAG(field1) OVER() and LEAD(field2) OVER() work?

Your SQL is here.

Colsai commented 3 years ago

Hi @xinxueHEAI , Regarding the question about Lag/Lead:

What do they do?

Other examples of possible window functions include: sum, count, avg, row_number, rank, dense_rank, ntile.

What should be inside OVER() ?

What if you want to LAG by 7 days instead of 1 day? Some complete query examples here would be great.

LAG (scalar_expression [,offset] [,default]) OVER ( [ partition_by_clause ] order_by_clause )

So, LAG(usage, 7) OVER (ORDER BY date asc) as lag_use, LEAD(usage,7) OVER (ORDER BY date asc) AS lead_use)

One important weakness of the functions mentioned in the articles I saw was that window functions can't be used with GROUP BY in their aggregations.

Some References: https://mode.com/sql-tutorial/sql-window-functions/ https://www.postgresql.org/docs/9.1/tutorial-window.html https://stackoverflow.com/questions/6218902/the-sql-over-clause-when-and-why-is-it-useful https://docs.microsoft.com/en-us/sql/t-sql/functions/lag-transact-sql?view=sql-server-ver15

xinxueHEAI commented 3 years ago

@wbfrench1 Could you work with Pratik (pmathur2@umbc.edu) and Srashti (ssoni1@umbc.edu) on when should I be using OVER(PARTITION BY field1 ORDER BY field2) in a query?

Say SUM(spending) OVER(PARTITION BY id ORDER BY date)

Please elaborate on your findings here.

wbfrench1 commented 3 years ago

Will do! We'll coordinate a time and put an answer together! Cheers! Barker

ssoni1 commented 3 years ago

test

xinxueHEAI commented 3 years ago

refer to this comment