SQL Running Total | Advance SQL | Rolling N months SUM, AVG, MIN, MAX
HTML-код
- Опубликовано: 3 фев 2022
- In this video we will learn how to find running/rolling calculations in SQL. This is very important concept and very frequently used in analytical projects.
Thank you for your helpful explanation of rolling calculations!
Amazing😍
select year(order_date), month(order_date), sum(sales)
, sum(sum(sales)) over(order by year(order_date), month(order_date)
rows between 2 preceding and current row)
, avg(sum(sales)) over(order by year(order_date), month(order_date)
rows between 2 preceding and current row)
, max(sum(sales)) over(order by year(order_date), month(order_date)
rows between 2 preceding and current row)
, min(sum(sales)) over(order by year(order_date), month(order_date)
rows between 2 preceding and current row)
from orders
group by year(order_date), month(order_date)
order by 1, 2
Great 🎉
Thank you so much. May you have all the success in your life 🙏🙏
Thank you 🙏
Awsome
Thanks for your clear instruction :D
You're welcome!
How to calculate the prior rolling 12 months average (current year = 2023, prior = 2022) -- I tried this: rows 24 preceding and 12 preceding
Very good explanation 🙂
Thank you 🙂
Good content. Can you please make more business scenario specific videos?
sure
Nice video 👍
Thank you 😊
Hello Ankit, My name is Fozan I'm from Pakistan , your videos are very helpful please also make assignment questions, so that we can test our understanding.
Hi Fozan, Good to know my Pakistan friends are getting some benefit out of my videos. I will do that 🙂
how to get dataset
9/142
Hi Ankit, A request... Could you please provide the schema of this query?
You can download superstore data from this link
drive.google.com/drive/mobile/folders/1Dc81McsB4lp1JUIwssDmmOaw6Z7rBK8T?usp=sharing
Hi Ankit, how can we find rolling sum without using any windows function . Today, One interviewer has asked me this question. If anyone has any idea can share their thoughts.
Can be done using self join
Does proceeding keyword is specific to Microsoft SQL Server or it would work across different RDBMS?
It works across most of the databases
@@ankitbansal6 thanks for quick reply!
Ankit bhai what does unbounded preceding and current following give.
Running sum
@@ankitbansal6 thank you
how can we apply the same logic in postgre please help
adding to above if there are year,month,weeks column
Also it will be very helpful if you can provide the data with each video to practice it also.
Okay
Hi Ankit, Could you please provide data for this tutorial?
I practice side by side as I go by the video. Thanks in advance.
You can download data from below link:
drive.google.com/drive/folders/1Dc81McsB4lp1JUIwssDmmOaw6Z7rBK8T?usp=sharing
In below video I have explained how to import the data:
ruclips.net/video/WIsPyYvEV2c/видео.html
@@ankitbansal6 thank you
I am working on writing a query which will give the number of hours left on starting day and it should gradually decrease towards end day and reach zero. Can you please create a video for this?
24- hour(sysdate)
with cte as (
select
datepart(year, order_date) as sales_year,
datepart(month, order_date) as sales_month,
sum(sales) as sales
From cust_orders
group by sales_year,sales_month)
select *,
sum(sales) over (order by sales_year asc, sales_month asc rows between 2 preceding and current row) as rolling_sum,
min(sales) over (order by sales_year asc, sales_month asc rows between 2 preceding and current row) as rolling_min,
max(sales) over (order by sales_year asc, sales_month asc rows between 2 preceding and current row) as rolling_max,
avg(sales) over (order by sales_year asc, sales_month asc rows between 2 preceding and current row) as rolling_avg
from cte;
please provide data ,create and insert stmt ,github or here
Provided in today's video
Please make a video on NTILE, LAG and LEAD function also
Sure
Please add the data
Sir Hume chotte bacche ko dekhna hai
Haha 😃
Kab dikhaoge