WITH cte1 AS (SELECT *, CASE WHEN Product is null THEN 0 ELSE 1 END flag, ROW_NUMBER() OVER() as rn FROM shop), cte2 AS (SELECT *, SUM(flag) OVER(ORDER BY rn) as cum_flag FROM cte1) SELECT FIRST_VALUE(product) OVER(PARTITION BY cum_flag) as product FROM cte2;
@select_star_by_anant_negiАй бұрын
works perfectly
@harshitsalecha221Ай бұрын
@@select_star_by_anant_negiThank you
@user-gq6cg3ls7fАй бұрын
another approach with cte as( select cx_name, concat(product, ',', LEAD(product) over (partition by cx_name order by cx_name)) next_row, ROW_NUMBER() over (partition by cx_name order by cx_name) RN from purchase_wed ) select count(case when next_row = ('Table,Chair') then 1 END) purchased_both, count(case when next_row = ('Table,') or next_row = ('Table,Table') then 1 END) purchased_table, count(case when next_row = ('Chair,') or next_row = ('Chair,Chair') then 1 END) purchased_chair from cte where RN=1
@select_star_by_anant_negiАй бұрын
What if someone purchased chair first and then table, next_row = ('Chair','Table')
@user-gq6cg3ls7fАй бұрын
@@select_star_by_anant_negi what do you want to display in a column as a output?
@select_star_by_anant_negiАй бұрын
1. How many customers ordered both table and chair? 2. How many customers ordered only table? 3. How many customers orderedonly chair?
@subarnaroyghatak.6590Ай бұрын
Nice explanation
@select_star_by_anant_negiАй бұрын
Thanks and welcome
@select_star_by_anant_negiАй бұрын
Instead of dates, try the question with timestamps to avoid discrepancies for the same date orders
@subarnaroyghatak.6590Ай бұрын
Yes
@user-gq6cg3ls7fАй бұрын
another approach with cte as( select *, case when transaction_type='Credit' then SUM(amount) over (partition by customer, transaction_type order by transaction_date rows between 1 preceding and current row) END Balance_1 from balance ) select customer, transaction_date, format(case when transaction_type='Debit' then lag(Balance_1) over (partition by customer order by transaction_date) - amount else Balance_1 END, '#,###,##') Balance from cte order by customer, transaction_type
@select_star_by_anant_negiАй бұрын
Yes, it works👍
@avisheksamaddar6133Ай бұрын
3
@select_star_by_anant_negiАй бұрын
Correct 💯
@subarnaroyghatak.6590Ай бұрын
Thanks for the video I was confused between range and rows frames, now a little bit more confident.
@select_star_by_anant_negiАй бұрын
Great to hear!
@subarnaroyghatak.6590Ай бұрын
Helpful
@select_star_by_anant_negiАй бұрын
Thanks 😊
@subarnaroyghatak.65902 ай бұрын
Nice explanation 👍
@select_star_by_anant_negi2 ай бұрын
@@subarnaroyghatak.6590 Thanks
@subarnaroyghatak.65902 ай бұрын
The case statement is new to me in the learning stage, but I got this very clearly. Thanks for the detailed explanation.
A4 : select * from student_result where marks > 80; A5 : select * from student_result where marks >= 85; A9 : select student_name, marks from student_result where marks < 50 or marks > 80 ;