diff --git a/problem1-1225-Report-Contiguos-Dates.sql b/problem1-1225-Report-Contiguos-Dates.sql new file mode 100644 index 0000000..2656bfc --- /dev/null +++ b/problem1-1225-Report-Contiguos-Dates.sql @@ -0,0 +1,43 @@ +WITH combine_states AS ( + SELECT + fail_date AS `date`, + 'failed' AS period_state + FROM Failed + where year(fail_date) = 2019 + + UNION ALL + + SELECT + success_date AS `date`, + 'succeeded' AS period_state + FROM Succeeded + where year(success_date) = 2019 +) + +, cte_based_on_state as ( + SELECT + `date`, + period_state, + CASE + WHEN LAG(period_state) OVER (ORDER BY date) = period_state THEN 0 + ELSE 1 + END AS state_change_flag + FROM combine_states + order by `date` +) + +,grouped_rank AS ( + SELECT + `date`, + period_state, + SUM(state_change_flag) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW) AS rnk + FROM cte_based_on_state +) + +select +period_state, +min(`date`) as start_date, +max(`date`) as end_date +from +grouped_rank +group by rnk \ No newline at end of file diff --git a/problem2-618-student-report-geography.sql b/problem2-618-student-report-geography.sql new file mode 100644 index 0000000..d2dc9db --- /dev/null +++ b/problem2-618-student-report-geography.sql @@ -0,0 +1,34 @@ +WITH am_con AS ( + SELECT + NAME AS America, + ROW_NUMBER() OVER (ORDER BY name) AS rnk + FROM Student + WHERE continent = 'America' +), +eu_con AS ( + SELECT + NAME AS Europe, + ROW_NUMBER() OVER (ORDER BY name) AS rnk + FROM Student + WHERE continent = 'Europe' +), +as_con AS ( + SELECT + NAME AS Asia, + ROW_NUMBER() OVER (ORDER BY name) AS rnk + FROM Student + WHERE continent = 'Asia' +) + +select + am_con.America, + as_con.Asia, + eu_con.Europe +from + am_con +left join + eu_con +on am_con.rnk = eu_con.rnk +left join + as_con +on am_con.rnk = as_con.rnk \ No newline at end of file diff --git a/problem3-615-average-salary-department-vs-company.sql b/problem3-615-average-salary-department-vs-company.sql new file mode 100644 index 0000000..92d2f1b --- /dev/null +++ b/problem3-615-average-salary-department-vs-company.sql @@ -0,0 +1,70 @@ +-- window function + +with cte as ( + select + e.department_id, + date_format(pay_date, '%Y-%m') as pay_month, + case + when + avg(amount) over (partition by date_format(pay_date, '%Y-%m')) + > avg(amount) over (partition by e.department_id, date_format(pay_date, '%Y-%m')) + then 'lower' + when + avg(amount) over (partition by date_format(pay_date, '%Y-%m')) + < avg(amount) over (partition by e.department_id, date_format(pay_date, '%Y-%m')) + then 'higher' + else + 'same' + end as 'comparison' + from salary as s + inner join + employee as e + on s.employee_id = e.employee_id +) + +select + pay_month, + department_id, + comparison +from cte +group by pay_month, department_id, comparison +order by pay_month, department_id + + + +--- with aggregation way + +with cte_monthly_avg as ( + select + date_format(pay_date, '%Y-%m') as pay_month, + avg(amount) as avg_amount + from + salary + group by date_format(pay_date, '%Y-%m') + +), + +cte_depart_monthly_avg as ( + select + e.department_id, + date_format(pay_date, '%Y-%m') as pay_month, + avg(amount) as dpt_avg_salary + from salary as s + inner join + employee as e + on s.employee_id = e.employee_id + group by date_format(pay_date, '%Y-%m'), e.department_id + +) + +select + cdma.pay_month, + cdma.department_id, + case + when cdma.dpt_avg_salary > cma.avg_amount then 'higher' + when cdma.dpt_avg_salary < cma.avg_amount then 'lower' + else 'same' + end as comparison +from cte_depart_monthly_avg as cdma +inner join cte_monthly_avg as cma + on cdma.pay_month = cma.pay_month \ No newline at end of file diff --git a/problem4-511-game-play-analysis.sql b/problem4-511-game-play-analysis.sql new file mode 100644 index 0000000..33f34e2 --- /dev/null +++ b/problem4-511-game-play-analysis.sql @@ -0,0 +1,6 @@ +select + player_id, + min(event_date) as first_login +from activity +group by player_id +order by player_id \ No newline at end of file