with cte as( select *,max(transaction_date)over(partition by user_id) as recent_trasn from transactions3) select user_id,recent_trasn,count(*) as purchase_count from cte where transaction_date=recent_trasn group by user_id,recent_trasn order by recent_trasn
@hairavyadav65793 ай бұрын
Nice explanations ,, bring more video related to data analyst role... Not only focus on sql,, if you make video related data analyst domain ,, your channel will grow and your way of explanation is very well and to attract more viewers also use Hindi to bring twist in video
@atharvjoshi99593 ай бұрын
with cte as(select * , dense_rank() over (partition by user_id order by transaction_date desc) as rnk, count(product_id) over (partition by user_id order by transaction_date desc) as purchase_count from transactions) select user_id, transaction_date, purchase_count from cte where rnk=1
@1112electronics3 ай бұрын
Great video and awesome explanation..thanks .. keep it up...🎉
@ishitvasingh99023 ай бұрын
We can also approach this by using Max agg function as a window function in cte and then call it out, with cte as ( select * , max(transaction_date) over (partition by user_id order by user_id) as recent_date from transactions) select transaction_date, user_id, count(product_id) as product_count from cte where transaction_date = recent_date group by 1,2 order by 1,2
@yamanthakur73523 ай бұрын
I tried through subqueries and 2 possible solutions: sol1: select ROW_NUMBER() Over(partition by user_id order by user_id asc) Rn, user_id,count(product_id) productCount, transaction_date from transactions Where transaction_date IN(select MAX(transaction_date) from transactions group by user_id ) group by user_id, transaction_date -------------------------------------------------------------------------------------- sol2: select * from (select RANK() OVER(Partition by user_id Order by transaction_date desc) RANK, user_id,count(product_id) prodcount, transaction_date from transactions group by user_id,transaction_date) TN Where TN.RANK=1
@gauravashish66053 ай бұрын
I have used this query select user_id,max(transaction_date), (select count(user_id) from transactions where user_id = a.user_id and transaction_date = max(a.transaction_date) group by user_id) as purchase_count from transactions a group by a.user_id order by max(transaction_date),USER_ID
@heathens28673 ай бұрын
Shower query
@chaitanyakadam57393 ай бұрын
Please continue this videos ❤❤❤❤
@manjunathbukkapatnam37313 ай бұрын
Hi @nishitha Nagar Can you please also post the create and insert script for the scenario based questions
@zerotohero5102 ай бұрын
Nice Explanation 😊
@echodelta76803 ай бұрын
Suppose there are two tables A and B. Each table has only two columns, ID and NAME. We have to get an output table that has rows in it as : 1st row from A 1st row from B 2nd row from A 2nd row from B 3rd row from A 3rd row from B .... and so on. What would be the query to generate this output table ?
@TheSaahil0063 ай бұрын
Add column id to both table. odd id to one table, even id to the second table... union both and order by id
@echodelta76803 ай бұрын
@@TheSaahil006 Thank you! I've been practicing complicated queries for so long that I forgot the simple things. There is another problem I am grappling with. Hope you can provide a simple solution for that too. Suppose a table EMP is as follows: ID DEPT 1 IT 2 NULL 3 NULL 4 HR 5 NULL 6 NULL How would we forward-fill the NULLs in column DEPT? I'm stuck with this problem since many days and the solutions I am coming across are not understandable to me. Kindly help.🙏
@TheSaahil0063 ай бұрын
@@echodelta7680 with cte as ( select id,dept,count(dept) over(partition by id order by id) as cliq from tab1 ) select id,dept,max(dept) over(partition by id,cliq) as new_dept from cte
@chandufighter76673 ай бұрын
Your explanation is newt level post post daily videos
@trendingthoughts67263 ай бұрын
❤❤❤ super videos
@neejudeshwal6515Ай бұрын
Thanks Mam
@hairavyadav65793 ай бұрын
And also bring a sql project which contain 15 questions 5 basic , 5 inter, 5 advn as per your experiences it give freshers some experiences
@sagartayde11853 ай бұрын
with cte as( select *, dense_rank() over(partition by user_id order by transaction_date desc) flag from transactions ) select transaction_date, user_id, sum(flag) as purchase_count from cte where flag=1 group by transaction_date, user_id order by transaction_date
@shubhamsharma86422 ай бұрын
select user_id,transaction_date,count(product_id) from (select *,rank()over(partition by user_id order by transaction_date desc)tp from transactionss)temp where tp=1 group by 1,2;
@arjundev49083 ай бұрын
with cte as(SELECT *, dense_rank()over(partition by user_id order by transaction_date desc) as rw FROM transactions) select transaction_date,user_id,count(distinct product_id) as purchase_count from cte where rw = 1 group by 1,2;
@sahasranamansriraman23093 ай бұрын
with user_transaction_cte as (select * , dense_rank() over (paartition by user_id order by transaction_date desc) as dense_rank from user_transactions), select transaction_date, user_id, count(user_id) as purchase_count from user_transaction_cte where dense_rank = 1 group by 1,2 select * from user_transaction_cte order by purchase_count ;
@varunas97843 ай бұрын
Thank you for sharing! Here's my attempt on SQL server: ================================== with cte as (select *, LAST_VALUE(transaction_date) over(partition by user_id order by user_id) [latest transaction], COUNT(product_id) over(partition by user_id, transaction_date order by transaction_date) [Count of products] from transactions11) select distinct transaction_date, user_id, [Count of products] from cte where transaction_date = [latest transaction] ==================================
@akshaykodekal84833 ай бұрын
Clearly explained
@Nishikant_N2 ай бұрын
Hi, Nishtha let me know is this Correct? SELECT user_id, transaction_date, Count(product_id) as Purchase_Count FROM ( SELECT product_id, user_id, spend, transaction_date, DENSE_RANK() OVER (PARTITION BY user_id ORDER BY transaction_date DESC) AS Latest_Date FROM Transactions ) AS RankedTransactions WHERE Latest_Date = 1 Group By user_id, transaction_date
@KapilKumar-hk9xk3 ай бұрын
with cte as (select *, dense_rank() over(partition by user_id order by date(transaction_date) desc) as dr from transactions) select transaction_date, user_id, count(product_id) as purchase_count from cte where dr = 1 group by transaction_date, user_id;
@HimanshuSingh-cf7wr3 ай бұрын
with m as ( select dense_rank() over (partition by user_id order by transaction_date desc) as ds_rnk, km.* from transactions as km) select distinct transaction_date,user_id,count(product_id) over (partition by user_id) as purchase_count from m where ds_rnk=1
@MitaliNeerPatel3 ай бұрын
Thank you , helpful
@aditi_shetti3 ай бұрын
My answer : with cte as (select product_id, user_id , transaction_date , rank() over (partition by user_id order by transaction_date desc) as rn from transactions) select user_id , transaction_date, count(product_id) as prod_count from cte where rn = 1 group by user_id , transaction_date
@vikassolanki2973 ай бұрын
With cte as( select distinct user_id, max(transaction_date) over( partition by user_id order by user_id) as latest_date from ttransactions) select latest_date, cte.user_id,count(product_id) from cte inner join ttransactions on cte.latest_date = ttransactions.transaction_date and cte.user_id = ttransactions.user_id group by latest_date,cte.user_id
@junkjunk7819Ай бұрын
No CTE nor subquery required. TC = amortized O of logN since internal implementation of max is done using minHeap algo due to order_by clause. im worst case scenario TC is O of n(with heapify) or nlogn(for single loading) SC = O of N as internally the data is stored in a tree or a heap SELECT MAX(transaction_date) OVER (PARTITION BY user_id) AS transaction_date, DISTINCT user_id, COUNT(product_id) OVER (PARTITION BY user_id ORDER BY transaction_date DESC LIMIT 1) AS purchase_count FROM transactions ORDER BY 1 asc;
@BabaiChakraborty-ss8pt3 ай бұрын
My Answer with recent_cte as ( select *, rank() over (PARTITION BY user_id ORDER BY transaction_date desc ) as recent_time from transactions order by user_id ) SELECT transaction_date, user_id, COUNT(product_id) AS purchased_counts FROM recent_cte WHERE recent_time = 1 GROUP BY transaction_date, user_id ORDER BY transaction_date;
@hairavyadav65793 ай бұрын
My approach select transaction_date,user_id,purchase_count from (select *,count(*) over(partition by user_id order by cast(transaction_date as date) desc) as purchase_count,row_number() over(partition by user_id order by cast(transaction_date as date) desc) rnk from transactions) sal where rnk = 1;
@Alexpudow3 ай бұрын
MS SQL approach SELECT TOP 3 transaction_date, user_id, count(product_id) FROM transactions GROUP BY transaction_date, user_id ORDER BY 1 DESC
@talkingtomsongs57913 ай бұрын
with purchase_count_per_date as ( select transaction_date, user_id, count(*) as purchase_count from transactions group by transaction_date, user_id order by transaction_date desc limit 3) select * from purchase_count_per_date pcd order by pcd.purchase_count;
@sachinn550312 күн бұрын
WITH CTE AS( SELECT USER_ID,COUNT(USER_ID) C, DATE(TRANSACTION_DATE) D FROM TRAN GROUP BY USER_ID,D ORDER BY USER_ID,D DESC), H AS ( SELECT *, ROW_NUMBER() OVER(PARTITION BY USER_ID ORDER BY D DESC) RECENT FROM CTE) SELECT USER_ID,C,D FROM H WHERE RECENT =1;
@astasingh3 ай бұрын
sql server my solution:- select transaction_date,user_id,count(user_id) from ( select user_id,transaction_date,dense_rank() over (partition by user_id order by transaction_date desc) ct from transactions) e where ct=1 group by user_id,transaction_date order by transaction_date
@Chathur7323 ай бұрын
select transaction_date,user_id,count(user_id) as purchase_count from ( select *, rank() over(partition by user_id order by transaction_date desc) as rn from transactions ) where rn = 1 group by transaction_date,user_id
@user-gq6cg3ls7f3 ай бұрын
another approach select transaction_date, user_id, count(*) purchase_count from transactions_Thu where transaction_date in( select max(transaction_date) transaction_date from transactions_Thu group by user_id) group by transaction_date, user_id order by transaction_date
@hrishi28393 ай бұрын
select max(transaction_date), user_id, rank() over(partition by max(transaction_date) order by user_id ) from transactions group by user_id order by user_id
@dhruvdhandhukiya80273 ай бұрын
select a.transaction_date, a.user_id, a.purchase_count from ( select transaction_date, user_id, count(product_id) as purchase_count, max(transaction_date) over (partition by user_id) as max_transaction_date from transactions_new group by (transaction_date), user_id order by transaction_date ) a where transaction_date = max_transaction_date
@shgg59963 ай бұрын
SELECT transaction_dt, user_id, COUNT(product_id) AS product_count FROM user_transaction GROUP BY transaction_dt, user_id ORDER BY transaction_dt DESC LIMIT 1;
@SnapMathShorts2 ай бұрын
with cte as ( select * , rank() over(partition by user_id order by transaction_date desc) as rnk from transactions) select transaction_date,user_id, count(*) as purchase_count from cte where rnk = 1 group by 1,2 order by 3