From 6aa894d9719639b30339738d140dd2b21f4d020b Mon Sep 17 00:00:00 2001 From: akanksha-vishwak Date: Sun, 1 Jun 2025 21:39:46 -0400 Subject: [PATCH 1/2] adding solution for Q4 --- 04-GamePlayAnalysis-I.sql | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 04-GamePlayAnalysis-I.sql diff --git a/04-GamePlayAnalysis-I.sql b/04-GamePlayAnalysis-I.sql new file mode 100644 index 0000000..675c5c9 --- /dev/null +++ b/04-GamePlayAnalysis-I.sql @@ -0,0 +1,16 @@ +-- Problem 4 : Game Play Analysis I (https://leetcode.com/problems/game-play-analysis-i/ ) + +select player_id, min(event_date) as 'first_login' from activity group by player_id +-- since this is group by it will return agg value as in one min + +select distinct player_id, min(event_date) over (partition by player_id) as 'first_login' from activity +-- for partition by one needs distinct because partition by returns all the rows, it will return 2 rows of player id 1 with same min date + +select distinct player_id, first_value(event_date) over (partition by player_id order by event_date) +as 'first_login' from activity +-- since we are using first_value, order by event date is important to pick 1st row + +select a.player_id, a.event_date as 'first_login' from ( + select b.player_id, b.event_date, + rank() over (partition by b.player_id order by b.event_date) as 'rnk' from activity b +) a where a.rnk = 1 From 5ee0d90042bade788e6448c39a19e06eeddfa381 Mon Sep 17 00:00:00 2001 From: akanksha-vishwak Date: Wed, 4 Jun 2025 00:49:10 -0400 Subject: [PATCH 2/2] solution to problem 3 --- 03-AvgSalaryDepartmentVsCompany.sql | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 03-AvgSalaryDepartmentVsCompany.sql diff --git a/03-AvgSalaryDepartmentVsCompany.sql b/03-AvgSalaryDepartmentVsCompany.sql new file mode 100644 index 0000000..c80dd7a --- /dev/null +++ b/03-AvgSalaryDepartmentVsCompany.sql @@ -0,0 +1,28 @@ +-- 3 Problem 3 : Average Salary Department vs Company (https://leetcode.com/problems/average-salary-departments-vs-company/solution/ ) + +with companyavg as ( + select + date_format(pay_date, '%y-%m') as pay_month, + avg(amount) as companyavg + from salary + group by pay_month +), +deptavg as ( + select + date_format(pay_date, '%y-%m') as pay_month, + department_id, + avg(amount) as departmentavg + from salary + join employee on salary.employee_id = employee.employee_id + group by department_id, pay_month +) +select + deptavg.pay_month, + department_id, + case + when departmentavg > companyavg then 'higher' + when departmentavg < companyavg then 'lower' + else 'same' + end as comparison +from companyavg +join deptavg on deptavg.pay_month = companyavg.pay_month