From 033daf8b255a22e4d896e08d827d0d36563da024 Mon Sep 17 00:00:00 2001 From: Rosaline Date: Thu, 2 Jan 2025 20:24:13 -0800 Subject: [PATCH] sql5 commit --- AvgSalaryDepVsComp.sql | 13 +++++++++++++ GamePlayAnalysis1.sql | 15 +++++++++++++++ ReportContiguosDates.sql | 9 +++++++++ StudentReportByGeography.sql | 14 ++++++++++++++ 4 files changed, 51 insertions(+) create mode 100644 AvgSalaryDepVsComp.sql create mode 100644 GamePlayAnalysis1.sql create mode 100644 ReportContiguosDates.sql create mode 100644 StudentReportByGeography.sql diff --git a/AvgSalaryDepVsComp.sql b/AvgSalaryDepVsComp.sql new file mode 100644 index 0000000..bef1e88 --- /dev/null +++ b/AvgSalaryDepVsComp.sql @@ -0,0 +1,13 @@ +select comp.pay_month, dep.department_id,( +case + when depsalary > compsalary then 'higher' + when depsalary < compsalary then 'lower' + else 'same' +end +) as comparison from +(select date_format(pay_date,'%Y-%m') as 'pay_month' ,employee_id, avg(amount)as 'compsalary' + from salary group by pay_month) as comp + join + (select date_format(pay_date,'%Y-%m') as 'pay_month', e.department_id, avg(amount) as 'depsalary' + from salary s join employee e on s.employee_id=e.employee_id group by e.department_id , pay_month) as dep + on dep.pay_month = comp.pay_month; \ No newline at end of file diff --git a/GamePlayAnalysis1.sql b/GamePlayAnalysis1.sql new file mode 100644 index 0000000..c1a7848 --- /dev/null +++ b/GamePlayAnalysis1.sql @@ -0,0 +1,15 @@ +--Solution1 +select player_id , min(event_date) as 'first_login' from activity group by player_id + +--Solution2 +select distinct player_id, min(event_date) over(partition by player_id) as 'first_login' from activity; + +--Solution3 +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; + +--Solution4 +select distinct a.player_id , first_value(event_date) over(partition by a.player_id order by event_date) as 'first_login' from activity a + +--Solution5 +select distinct a.player_id , last_value(event_date) over(partition by a.player_id order by event_date desc +range between unbounded preceding and unbounded following) as 'first_login' from activity a diff --git a/ReportContiguosDates.sql b/ReportContiguosDates.sql new file mode 100644 index 0000000..5159bac --- /dev/null +++ b/ReportContiguosDates.sql @@ -0,0 +1,9 @@ +# Write your MySQL query statement below +with cte as (select fail_date as 'dat' , 'failed' as period_state, rank() over (order by fail_date) as 'rnk' from failed where year(fail_date) = 2019 +union all +select success_date as 'dat', 'succeeded' as period_state, rank() over (order by success_date) as 'rnk' from succeeded where year(success_date) = 2019 +) + +select period_state, min(dat) as 'start_date', max(dat) as 'end_date' from ( + select * , (rank() over (order by dat) - rnk) as 'group_rank' from cte) as y + group by group_rank , period_state order by start_date; \ No newline at end of file diff --git a/StudentReportByGeography.sql b/StudentReportByGeography.sql new file mode 100644 index 0000000..24e6aa3 --- /dev/null +++ b/StudentReportByGeography.sql @@ -0,0 +1,14 @@ +with cte as + (select name as 'America', row_number() over (order by name) as 'rnk' from student + where continent='America' order by name), + +cte1 as + (select name as 'Asia', row_number() over (order by name) as 'rnk' from student + where continent='Asia' order by name), + +cte2 as (select name as 'Europe', row_number() over (order by name) as 'rnk' from student +where continent='Europe' order by name) + +select America, Asia , Europe from cte1 + right join cte on cte.rnk=cte1.rnk + left join cte2 on cte.rnk=cte2.rnk; \ No newline at end of file