From 02d2a725547bf456f726fbb0533777ea937453fc Mon Sep 17 00:00:00 2001 From: Kaustubhk28 <99148707+Kaustubhk28@users.noreply.github.com> Date: Fri, 3 Jan 2025 19:05:59 -0500 Subject: [PATCH] Done Mock_SQL2 --- Mock_SQL2.sql | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Mock_SQL2.sql diff --git a/Mock_SQL2.sql b/Mock_SQL2.sql new file mode 100644 index 0000000..0027274 --- /dev/null +++ b/Mock_SQL2.sql @@ -0,0 +1,21 @@ +# Solution +with cte1 as +( + select u.user_id, u.favorite_brand, i.item_id, i.item_brand, o.order_date, + rank() over(partition by u.user_id order by o.order_date) as ranks + from Users u + left join Orders o + on u.user_id = o.seller_id + left join Items i + on i.item_id = o.item_id +), cte2 as +( + select * + from cte1 + where ranks = 2 and favorite_brand = item_brand +) +select c1.user_id as seller_id, (case when count(c2.ranks) >= 2 then 'yes' else 'no' end) as '2nd_item_fav_brand' +from cte1 c1 +left join cte2 c2 +on c1.user_id = c2.user_id +group by seller_id \ No newline at end of file