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
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
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
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
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
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
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
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;
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] ==================================
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
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;
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;
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
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;
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
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
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;
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 ;
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
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 ?
@@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.🙏
@@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
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
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;
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
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
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
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;
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
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
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
Nice Explanation 😊
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
Shower query
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
Great video and awesome explanation..thanks .. keep it up...🎉
Please continue this videos ❤❤❤❤
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
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
❤❤❤ super videos
Clearly explained
Your explanation is newt level post post daily videos
Thank you , helpful
Hi @nishitha Nagar
Can you please also post the create and insert script for the scenario based questions
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
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;
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]
==================================
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
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;
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
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;
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
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;
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
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
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;
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 ;
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
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
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 ?
Add column id to both table. odd id to one table, even id to the second table... union both and order by id
@@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.🙏
@@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
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
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;
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
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
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
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;
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