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
8 changes: 8 additions & 0 deletions Week3/solutions/hw2_task1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
select a.title as album_name,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Принято

count(*) as cnt
from track t
join album a
on t.album_id = a.album_id
group
by a.album_id
having (avg(t.milliseconds) > 250000);
83 changes: 83 additions & 0 deletions Week3/solutions/hw2_task10.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
with t1 as (
select a.album_id as album_id,
a.title as album_name,
mt.name as mt_name,
sum(t.bytes) as size_per_mt,
a.artist_id as artist_id
from album a
join track t
on a.album_id = t.album_id
join media_type mt
on t.media_type_id = mt.media_type_id
group by a.album_id, a.title, mt.name
),
t2 as (
select t1.album_id as album_id,
t1.artist_id as artist_id,
max(t1.size_per_mt) as max_size
from t1
group by t1.album_id, t1.artist_id
),
t3 as (
select t1.album_id as album_id,
t1.album_name as album_name,
t1.mt_name as mt_name,
t1.artist_id as artist_id
from t2
join t1
on (t2.album_id, t2.max_size) = (t1.album_id, t1.size_per_mt)
),
t4 as (
select ar.artist_id as artist_id,
ar.name as artist_name,
t3.mt_name as mt_name
from artist ar
left join t3
on ar.artist_id = t3.artist_id
),
t5 as (
select t4.artist_id as artist_id,
t4.artist_name as artist_name,
count(*) as whole_cnt
from t4
group by t4.artist_id, t4.artist_name
),
t6 as (
select t4.artist_id as artist_id,
t4.artist_name as artist_name,
t4.mt_name as mt_name,
count(*) as media_cnt
from t4
group by t4.artist_id, t4.artist_name, t4.mt_name
),
t7 as (
select t6.artist_id as artist_id,
t6.artist_name as artist_name,
max(t6.media_cnt) as media_cnt
from t6
group by t6.artist_id, t6.artist_name
),
t8 as (
select distinct on (t6.mt_name, t6.artist_id) t6.mt_name as mt_name,
t6.artist_id as artist_id,
t6.artist_name as artist_name
from t6
join t7
on (t7.artist_id, t7.media_cnt) = (t6.artist_id, t6.media_cnt)
order by t6.mt_name, t6.artist_id
)
select t8.artist_name as artist_name,
t8.mt_name as media_type_name,
CASE
when (t8.mt_name is null) then 0
else t7.media_cnt
end as media_cnt,
CASE
when (t8.mt_name is null) then 0
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему в ответе есть 0 с media_cnt ?
Пример такого альбома

select * 
  from track
where album_id in (
select album_id
  from album
 where artist_id in (
         select artist_id
         from artist where name = 'Gustav Mahler')
)```

Хотя тип медиа здесь явно определен

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Для 'Gustav Mahler' у меня 0 не выводится. 0 выводится для тех артистов, у которых нет ни одного альбома.

Вопрос по формулировке задания: "Для каждого альбома тип медиа определяется как наибольшая суммарный размер треков по жанру". Возможно, здесь допущена опечатка и тип медиа определяется как наибольший суммарный размер треков по типу медиа? Я так и сделал.

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Принято

else t5.whole_cnt
end as whole_cnt
from t8
join t7
on t7.artist_id = t8.artist_id
join t5
on t5.artist_id = t8.artist_id;
10 changes: 10 additions & 0 deletions Week3/solutions/hw2_task2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
select g.name as g_name,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Принято

mt.name as mt_name,
count(*) as cnt
from track t
join genre g
on g.genre_id = t.genre_id
join media_type mt
on mt.media_type_id = t.media_type_id
group
by (g.genre_id, mt.media_type_id);
12 changes: 12 additions & 0 deletions Week3/solutions/hw2_task3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
select e1.last_name || ' ' || e1.first_name as emp_full_name,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Принято

e1.birth_date as emp_birth_date,
e1.hire_date as epm_hire_date,
e2.last_name || ' ' || e2.first_name as manager_full_name,
e2.birth_date as manager_birth_date,
e2.hire_date as manager_hire_date
from employee e1
inner
join employee e2
on e1.reports_to = e2.employee_id
where (e1.birth_date < e2.birth_date)
and (e1.hire_date > e2.hire_date);
19 changes: 19 additions & 0 deletions Week3/solutions/hw2_task4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
with artists_id_and_count as
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Принято

(
select a.artist_id as ar_id,
count(*) as cnt
from track t
join album a
on a.album_id = t.album_id
where t.genre_id in (
select genre_id
from genre
where name in ('Rock', 'Metal')
)
group by a.artist_id
)
select art.name as artist_name,
c.cnt as qty
from artist art
join artists_id_and_count c
on c.ar_id = art.artist_id;
14 changes: 14 additions & 0 deletions Week3/solutions/hw2_task5.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
select e.last_name || ' ' || e.first_name as empl_full_name,
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Подумайте, что будет, если сотрудники полные тезки

count(distinct c.customer_id) as customer_cnt,
count(distinct i.invoice_id) as invoice_cnt,
count(distinct i.billing_address) as addresses_cnt,
sum(i_l.quantity) as tracks_cnt
from customer c
join employee e
on c.support_rep_id = e.employee_id
join invoice i
on c.customer_id = i.customer_id
join invoice_line i_l
on i.invoice_id = i_l.invoice_id
group
by e.employee_id;
21 changes: 21 additions & 0 deletions Week3/solutions/hw2_task6.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
select CASE
when (length(c.last_name) - length(REPLACE(lower(c.last_name), 'a', '')) = 0) then 'A'
when (length(c.last_name) - length(REPLACE(lower(c.last_name), 'a', '')) = 1) then 'B'
when (length(c.last_name) - length(REPLACE(lower(c.last_name), 'a', '')) = 2) then 'C'
when (length(c.last_name) - length(REPLACE(lower(c.last_name), 'a', '')) >= 3) then 'D'
end as segment_group,
count(distinct mt.name) as cnt
from customer c
join invoice i
on c.customer_id = i.customer_id
join invoice_line i_l
on i.invoice_id = i_l.invoice_id
join track t
on i_l.track_id = t.track_id
join genre g
on t.genre_id = g.genre_id
join media_type mt
on mt.media_type_id = t.media_type_id
where lower(g.name) like '%metal%'
group
by segment_group;
50 changes: 50 additions & 0 deletions Week3/solutions/hw2_task7.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
with t1 as (
select g.name as g_name,
c.company as company_name,
sum(i_l.quantity) as tracks_per_comp_and_genre,
t.name as track_name
from customer c
join invoice i
on c.customer_id = i.customer_id
join invoice_line i_l
on i.invoice_id = i_l.invoice_id
join track t
on i_l.track_id = t.track_id
join genre g
on t.genre_id = g.genre_id
where company is not null
group by g.name, c.company, t.track_id
), t2 as (
select t1.g_name as g_name,
t1.company_name as company_name,
sum(t1.tracks_per_comp_and_genre) as sum_tracks
from t1
group by t1.g_name, t1.company_name
), t3 as (
select t2.g_name as g_name,
max(t2.sum_tracks) as max_per_genre
from t2
group by t2.g_name
), t4 as (
select distinct on (t2.g_name) t2.g_name, t2.company_name
from t2
join t3
on (t2.g_name, t2.sum_tracks) = (t3.g_name, t3.max_per_genre)
order
by t2.g_name, t2.company_name
), t5 as (
select t1.g_name,
t1.company_name,
t1.tracks_per_comp_and_genre
from t4
join t1
on (t1.g_name, t1.company_name) = (t4.g_name, t4.company_name)
)
select res.company_name, res.track_name
from (
select distinct on (t1.g_name) t1.g_name, t1.company_name, t1.track_name
from t1
where (t1.g_name, t1.company_name, t1.tracks_per_comp_and_genre)
in (select * from t5)
order by t1.g_name, t1.track_name
) as res;
43 changes: 43 additions & 0 deletions Week3/solutions/hw2_task8.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
with t1 as (
select t.track_id as track_id,
t.name as track_name,
g.name as genre_name,
sum(il.quantity) as purchases_of_track
from track t
join genre g
on t.genre_id = g.genre_id
join invoice_line il
on t.track_id = il.track_id
group by t.track_id, t.name, g.name
), t2 as (
select t1.genre_name as genre_name,
sum(t1.purchases_of_track) as sum_per_genre
from t1
group
by t1.genre_name
), t3 as (
select t1.genre_name as genre_name,
t1.track_id as track_id,
t1.track_name as track_name,
t1.purchases_of_track / t2.sum_per_genre * 100 as popularity,
row_number() over (partition by t1.genre_name order by t1.genre_name, t1.track_name) as num
from t1
join t2
on t1.genre_name = t2.genre_name
order by t1.genre_name, t1.track_name
), t4 as (
select cp1.genre_name,
cp1.track_id as track_id,
cp1.track_name as track_name,
sum(cp2.popularity) as gap
from t3 cp1
join t3 cp2
on cp1.genre_name = cp2.genre_name and cp1.num >= cp2.num
group by cp1.track_id, cp1.track_name, cp1.genre_name
)
select t4.track_name as track_name,
t3.popularity as popularity
from t4
join t3
on t3.track_id = t4.track_id
where t4.gap <= 25;
49 changes: 49 additions & 0 deletions Week3/solutions/hw2_task9.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
with t1 as (
select a.album_id as album_id,
a.title as album_title,
t.milliseconds as track_duration,
g.name as genre_name
from album a
join track t
on a.album_id = t.album_id
join genre g
on t.genre_id = g.genre_id
), t2 as (
select t1.album_id as album_id,
t1.album_title as album_title,
t1.genre_name as genre_name,
sum(t1.track_duration) as duration_per_genre
from t1
group by t1.album_id, t1.album_title, t1.genre_name
), t3 as (
select t2.album_id as album_id,
t2.album_title as album_title,
max(t2.duration_per_genre) as max_duration
from t2
group by t2.album_id, t2.album_title
), t4 as (
select t3.album_id as album_id,
t3.album_title as album_title,
t2.genre_name as genre_name
from t3
join t2
on (t3.album_id, t3.max_duration) =
(t2.album_id, t2.duration_per_genre)
), t5 as (
select a.album_id as album_id,
a.title as album_title,
sum(il.quantity) as cnt
from album a
join track t
on a.album_id = t.album_id
join invoice_line il
on t.track_id = il.track_id
group by a.album_id, a.title
)
select t4.album_title as album_name,
t4.genre_name as genre_name,
coalesce(t5.cnt, 0) as cnt
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Почему в ответе есть альбомы с 0 в количестве треков. Пример альбом Allegri: Miserere

Copy link
Copy Markdown
Author

@PlotnikovAleksey PlotnikovAleksey Mar 29, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Потому что cnt показывает сколько треков было куплено в альбоме, а в альбоме Allegri: Miserere и некоторых других ни одного трека не купили (нет ни одного invoice_line c track_id тех треков, которые есть в данных альбомах)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Принято

from t4
left
join t5
on t5.album_id = t4.album_id;
13 changes: 13 additions & 0 deletions Week4/solutions/hw4_task1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
select distinct on (art.artist_id, g.name) art.name as artist_name,
g.name as genre_name,
t.name as track_name,
t.milliseconds as track_length
from artist art
join album a
on art.artist_id = a.artist_id
join track t
on a.album_id = t.album_id
join genre g
on t.genre_id = g.genre_id
order
by art.artist_id, g.name, t.milliseconds desc;
24 changes: 24 additions & 0 deletions Week4/solutions/hw4_task2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
with t1 as (select c.company as company_name,
t.name as track_name,
g.name as genre_name,
a.title as album_name,
il.quantity * il.unit_price as price
from customer c
join invoice i
on c.customer_id = i.customer_id
join invoice_line il
on i.invoice_id = il.invoice_id
join track t
on il.track_id = t.track_id
join genre g
on t.genre_id = g.genre_id
join album a
on t.album_id = a.album_id
where c.company is not null
)
select distinct on (t1.company_name) t1.company_name as company_name,
t1.track_name as track_name,
t1.genre_name as genre_name,
t1.album_name as album_name
from t1
order by t1.company_name, t1.price desc, t1.track_name;
28 changes: 28 additions & 0 deletions Week4/solutions/hw4_task3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
with t1 as (select distinct on (hire_date)
hire_date as hire_date,
coalesce(
lag(hire_date) over (order by hire_date),
to_timestamp('1998-12-31', 'YYYY-MM-DD')
) as previous_date
from employee
order by hire_date
), t2 as (
select t1.hire_date as hire_date,
t1.previous_date as previous_date
from t1
where t1.hire_date - t1.previous_date > '1 day'
), t3 as (
select t2.hire_date as hire_date,
t2.previous_date as previous_date,
coalesce(
lead(t2.hire_date) over (order by t2.hire_date),
to_timestamp('2020-01-02', 'YYYY-MM-DD')
) as next_date
from t2
)
select coalesce(t_1.previous_date, t_2.hire_date) + interval '1 day' as date_start,
coalesce(t_1.hire_date, t_2.next_date) - interval '1 day' as date_end
from t3 t_1
full
join t3 t_2
on t_1.hire_date = t_2.next_date;
Loading