Problem with Running SUM in SQL | Watch it to Avoid The Mistake

  Рет қаралды 22,804

Ankit Bansal

Ankit Bansal

Күн бұрын

Пікірлер: 47
@vandanasharma4738
@vandanasharma4738 Жыл бұрын
unbounded preceding -> means window starting the first row of the resultset current row -> means current row for which you are calculating value unbounded Following -> means window ending the last row of the resultset select *, sum(cost) over (order by cost asc) --duplicates causing issue from products; --method 1 select *, sum(cost) over (order by cost asc, product_id ) --use another key column for unique records during sort and summation from products; --method 2 select * , sum(cost) over (order by cost asc rows between unbounded preceding and current row) from products;
@lamborghiniveneno8423
@lamborghiniveneno8423 Ай бұрын
3 minutes 42 seconds... Totally worth it!!!! Thanks Ankit!! Always a gem!!
@ShivaKumarTeam
@ShivaKumarTeam 2 жыл бұрын
Table for above example create table products ( product_id varchar(20), cost int) insert into products values ('P1', 200), ('P2', 300), ('P3', 300), ('P4', 500), ('P5', 800)
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Thank you 😊
@mokshjaiswal3347
@mokshjaiswal3347 Жыл бұрын
Your video was short as well as easy to understand. Very helpful video.
@parveen8122
@parveen8122 2 жыл бұрын
Running cost without window function. btw Window fn are more simple select p1.product_id, sum(p2.cost) from products p1 left join products p2 on p1.product_id>= p2.product_id group by p1.product_id
@akshobshekhar736
@akshobshekhar736 2 ай бұрын
Hi Ankit we can simply use "rows between unbounded preceding and current row" without order by that also does the trick
@sharu164
@sharu164 2 жыл бұрын
Thank you Ankit for explaining in such simple terminologies, it makes it so much more interesting & relatable 😇
@SACHINKUMAR-px8kq
@SACHINKUMAR-px8kq Жыл бұрын
Thankyou so much sir , I watched your All Videos on SQL sir , Thankyou so much sir 😍 Feel Like SQL PRo
@ankitbansal6
@ankitbansal6 Жыл бұрын
Keep watching
@abhishek_grd
@abhishek_grd 2 жыл бұрын
Great Concept. Can you please make video on lead, lag functions use cases as well and more on unbounded precedence?
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Okay
@adityabaha
@adityabaha 2 жыл бұрын
Hi Ankit, Thanks for this beautiful piece of tip. Could you please create a video to explain the concept of "rows between unbounded preceding and current row" with some more options please?? Thanks in Advance 😀
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Sure 😊
@adityabaha
@adityabaha 2 жыл бұрын
@@ankitbansal6 Eagerly waiting for this 🙂
@sumibits
@sumibits 2 жыл бұрын
Hello Ankit, Quick question... do you take SQL online class ? If Yes, I am interested. If No, Can I grab your 30 mins on calendar ? I am looking for some guidance from you. Thank you
@ankitbansal6
@ankitbansal6 2 жыл бұрын
You can block my calendar.
@hairavyadav6579
@hairavyadav6579 Ай бұрын
Today i learn new things
@taniyasaini6830
@taniyasaini6830 2 жыл бұрын
Awesome! Waiting for the next video :)
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Thank you ☺️
@chirodiplodhchoudhury7222
@chirodiplodhchoudhury7222 2 жыл бұрын
Hey Ankit , can you make a video about how to calculate running difference? Also concepts like Rows and Range , unbounded preceding following etc on window functions?
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Check this out kzbin.info/www/bejne/a3rKmZ2VZ7V6jJI
@vandanasharma4738
@vandanasharma4738 Жыл бұрын
unbounded preceding -> means window starting the first row of the resultset current row -> means current row for which you are calculating value unbounded Following -> means window ending the last row of the resultset
@SouravKumar-vn7pp
@SouravKumar-vn7pp 2 жыл бұрын
with cte as ( select *, rank() over (order by product_id) as rnk from prod ) select cte.*, sum(cost) over(order by rnk asc) as running_cost from cte
@percyjackson1662
@percyjackson1662 2 жыл бұрын
Hi Ankit, Thanks for posting such helpful videos 💯💯. On difference b/w just order by and order by rows , mysql doesnt create any issues. Im getting same result [running_sum_1, running_sum_2]. Am i missing out anything here? with products as ( select * from ( select 'p1' as prd_id, 200 as cost union select 'p2' as prd_id, 300 as cost union select 'p3' as prd_id, 300 as cost union select 'p4' as prd_id, 500 as cost union select 'p5' as prd_id, 800 as cost ) as x ) select * , sum(cost) over (order by prd_id) as running_sum_1 , sum(cost) over (order by prd_id rows BETWEEN UNBOUNDED PRECEDING and 0 PRECEDING) as running_sum_2 from products
@madhusudhangoud9296
@madhusudhangoud9296 2 жыл бұрын
Hi Ankit, the second logic which you explained will work only in mysql ? or will it work on hive?
@ankitbansal6
@ankitbansal6 2 жыл бұрын
It should work in hive
@innominatesoloist1597
@innominatesoloist1597 2 жыл бұрын
wow..................thank you so much
@Paras7627
@Paras7627 7 ай бұрын
Hello Ankit, Can't we use distinct function just to get the unique values along with the running sum function, in the problem?
@ankitbansal6
@ankitbansal6 7 ай бұрын
Nope. It will produce different results because of order of execution. Try out
@vijayvishwakarma2293
@vijayvishwakarma2293 2 жыл бұрын
Please bro make a video purchase and sale unit fifo method how to implement in SQL
@ankitbansal6
@ankitbansal6 2 жыл бұрын
Please explain the problem with some data
@vijayvishwakarma2293
@vijayvishwakarma2293 2 жыл бұрын
@@ankitbansal6 please bro send your emails I send you screenshot and details
@ankitbansal6
@ankitbansal6 2 жыл бұрын
ankitbansal1988@gmail.com
@vijayvishwakarma2293
@vijayvishwakarma2293 2 жыл бұрын
@@ankitbansal6 thanks bro please help me I will share shortly
@vijayvishwakarma2293
@vijayvishwakarma2293 2 жыл бұрын
@@ankitbansal6 Bhai aap check karo mai send kar diya h
@vansh9899
@vansh9899 6 ай бұрын
what if cost is not in ascending order?
@akshayjoshi4106
@akshayjoshi4106 Жыл бұрын
Can we use order by rowid
@rk-ej9ep
@rk-ej9ep 2 жыл бұрын
Awesome
@clouddrive6833
@clouddrive6833 Жыл бұрын
That was perfect
@adityabaha
@adityabaha Жыл бұрын
Hi @Ankit, I've come across interview questions around Running Avg. , Could you please explain its relevance & how we can achieve it? Also, can there be other rolling aggregations? Could you pls make a video on this topic if you think this will be helpful to all? Thanks for everything you do for us!! 🙂
@ankitbansal6
@ankitbansal6 Жыл бұрын
Like running sum you can do running average. Just use the avg function instead of sum.
@adityabaha
@adityabaha Жыл бұрын
Thank you@@ankitbansal6
@vishalsonawane.8905
@vishalsonawane.8905 6 ай бұрын
Done
@mohit231
@mohit231 2 жыл бұрын
Didn’t know about it
@ankitbansal6
@ankitbansal6 2 жыл бұрын
But now you know 😊
@ks97626
@ks97626 11 күн бұрын
Awesome
Motorbike Smashes Into Porsche! 😱
00:15
Caters Clips
Рет қаралды 23 МЛН
Osman Kalyoncu Sonu Üzücü Saddest Videos Dream Engine 275 #shorts
00:29
Hoodie gets wicked makeover! 😲
00:47
Justin Flom
Рет қаралды 88 МЛН
All About SQL Aggregations | SQL Advance | Zero to Hero
17:43
Ankit Bansal
Рет қаралды 58 М.
Swiggy Data Analyst SQL Interview Question and Answer
17:05
Ankit Bansal
Рет қаралды 20 М.
Lead/Lag Window Analytical functions in SQL | Advance SQL concepts
10:34
Motorbike Smashes Into Porsche! 😱
00:15
Caters Clips
Рет қаралды 23 МЛН