Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions problem1-1225-Report-Contiguos-Dates.sql
Original file line number Diff line number Diff line change
@@ -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
34 changes: 34 additions & 0 deletions problem2-618-student-report-geography.sql
Original file line number Diff line number Diff line change
@@ -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
70 changes: 70 additions & 0 deletions problem3-615-average-salary-department-vs-company.sql
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions problem4-511-game-play-analysis.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
select
player_id,
min(event_date) as first_login
from activity
group by player_id
order by player_id