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
Binary file added .DS_Store
Binary file not shown.
Binary file modified Week1/.DS_Store
Binary file not shown.
Binary file added Week1/solutions/.DS_Store
Binary file not shown.
67 changes: 67 additions & 0 deletions Week1/solutions/hw1_dorzhiev_ardan/coffeshop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
echo 'Welcome to the Dave Coffeeshop!'

if [ -f davecoffeeshop.db ]; then
echo 'davecoffeeshop.db exists, using existing database'
else
echo 'davecoffeeshop.db not exists => creating db file'
touch davecoffeeshop.db

echo 'success'
fi

echo 'To log order, enter LOG
To end session, enter EOS
To end business day, enter EOD'

while read command
do
if [ $command = 'LOG' ]; then
echo 'Enter Barista'
read barista
echo 'Enter Drink'
read in_drink
echo "Barista name: $barista"
echo "Drinking: $in_drink"

case $in_drink in
"AME")
price=100
;;
"ESP")
price=120
;;
"CAP")
price=150
;;
"LAT")
price=170
;;
"RAF")
price=200
;;
*)
price=0
;;
esac

if (( price > 0 )); then
echo "$barista;$in_drink;$price" >> davecoffeeshop.db
echo 'Order was saved!'
else
echo Invalid drink c, ignoring
fi

elif [ $command = 'EOS' ]; then
echo 'End of Session'
exit
elif [ $command = 'EOD' ]; then
echo 'End of Day!'

cat davecoffeeshop.db | sort -nk1 -t ";" | awk -F";" 'BEGIN {OFS=";" }\
($1 == last || last == "") {sum += $3} ($1 != last && last != "")\
{print last, sum; sum = $3} {last = $1} END {print last, sum}' | sort -nrk2 -t ";"

else
echo 'WRONG COMMAND. USE ONLY LOG, EOD, EOS'
fi
done
4 changes: 4 additions & 0 deletions Week1/solutions/hw1_dorzhiev_ardan/davecoffeeshop.db
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ARDAN;AME;100
MARIE;RAF;200
ARDAN;CAP;150
MARIE;LAT;170
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(base) MacBook-Air-Ardan:~ ardandorzhiev$ sh ./coffeshop.sh
Welcome to the Dave Coffeeshop!
davecoffeeshop.db not exists => creating db file
success
To log order, enter LOG
To end session, enter EOS
To end business day, enter EOD
LOG
Enter Barista
ARDAN
Enter Drink
AME
Barista name: ARDAN
Drinking: AME
Order was saved!
LOG
Enter Barista
MARIE
Enter Drink
RAF
Barista name: MARIE
Drinking: RAF
Order was saved!
LOG
Enter Barista
ARDAN
Enter Drink
CAP
Barista name: ARDAN
Drinking: CAP
Order was saved!
LOG
Enter Barista
NICK
Enter Drink
COLA
Barista name: NICK
Drinking: COLA
Invalid drink c, ignoring
LOG
Enter Barista
MARIE
Enter Drink
LAT
Barista name: MARIE
Drinking: LAT
Order was saved!
EOD
End of Day!
MARIE;370
ARDAN;250
EOS
End of Session
(base) MacBook-Air-Ardan:~ ardandorzhiev$ cat davecoffeeshop.db
ARDAN;AME;100
MARIE;RAF;200
ARDAN;CAP;150
MARIE;LAT;170
(base) MacBook-Air-Ardan:~ ardandorzhiev$
Binary file added Week3/.DS_Store
Binary file not shown.
Binary file added Week3/solutions/.DS_Store
Binary file not shown.
24 changes: 24 additions & 0 deletions Week3/solutions/hw2_dorzhiev_ardan/hw2_task1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Вывести все альбомы со средней длинной трека, большей 250 секунд.
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.

Принято

* Вывести название альбома и количество треков. (album_name, cnt).*/


with album_track_length as (select a.album_id
, a.title as album_name
, round(avg(t.milliseconds)/1000,0) avg_track_length_in_seconds
from album a
join track t on a.album_id = t.album_id
group by a.album_id
, a.title
having avg(t.milliseconds)/1000 > 250
order by a.album_id )

select a.title as album_name
, count(t.track_id) cnt
from album a
join track t on a.album_id = t.album_id
where exists (select 1 from album_track_length atl where atl.album_id = a.album_id)
group by a.album_id
, a.title
order by a.title


76 changes: 76 additions & 0 deletions Week3/solutions/hw2_dorzhiev_ardan/hw2_task10.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* Для каждого альбома тип медиа определяется как наибольшая суммарный размер треков по жанру.
* Для каждого исполнителя вывести тип медиа, соответствующий наибольшему количеству типов медиа по альбомам, где они участвуют.
* Вывести имя исполнителя, тип медиа, количество альбомов с данными типом, общее количество альбомов.
*
* (artist_name, media_type_name, media_cnt, whole_cnt)
*
* */


with album_agg as (select a.album_id
, t.media_type_id
, sum(t.bytes) size_tracks
from album a
join track t on t.album_id = a.album_id
group by a.album_id
, t.media_type_id
order by a.album_id
) ,
album_media as (select a1.album_id
, a1.media_type_id
from album_agg a1
join album a on a1.album_id = a.album_id
where (a1.album_id, a1.media_type_id) in (select a2.album_id, a2.media_type_id
from album_agg a2
where a1.album_id = a2.album_id
and a1.media_type_id = a2.media_type_id
order by a2.size_tracks desc
limit 1
)


) ,

artist_albums_media as ( select a.artist_id
, am.media_type_id
, count(distinct al.album_id) cnt_albums
from artist a
join album al on al.album_id = a.artist_id
join album_media am on al.album_id = am.album_id
group by a.artist_id
, am.media_type_id
) ,

artist_cnt_media as (select ar.artist_id
, m.media_type_id
, count(distinct a.album_id) cnt_media_albums
from album a
join album_media m on a.album_id = m.album_id
join artist ar on a.artist_id = ar.artist_id
group by ar.artist_id
, m.media_type_id
) ,

artist_cnt_albums as (select ar.artist_id
, count(distinct a.album_id) cnt_albums
from album a
join artist ar on a.artist_id = ar.artist_id
group by ar.artist_id
)


select a."name" as artist_name
, mt."name" as media_type_name
, acm.cnt_media_albums as media_cnt
, aca.cnt_albums as whole_cnt
from artist_albums_media am
join artist_cnt_media acm on am.artist_id = acm.artist_id
join artist_cnt_albums aca on am.artist_id = aca.artist_id
join artist a on am.artist_id = a.artist_id
join media_type mt on am.media_type_id = mt.media_type_id
where am.cnt_albums in (select max(am1.cnt_albums) c
from artist_albums_media am1
where am1.artist_id = am.artist_id)



17 changes: 17 additions & 0 deletions Week3/solutions/hw2_dorzhiev_ardan/hw2_task2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/* Для каждого набора жанр, тип медиа вывести количество треков.
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.

Принято

* Если для набора жанр, тип медиа треков нет, то такой набор выводить не требуется.
* (genre_name, media_type_name, cnt)
*/

select g."name" as genre_name
, mt."name" as media_type_name
, count(t.track_id) as cnt_tracks
from track t
join media_type mt on t.media_type_id = mt.media_type_id
join genre g on t.genre_id = g.genre_id
group by g.genre_id
, g."name"
, mt.media_type_id
, mt."name"
having count(t.track_id) > 0
order by g."name"
16 changes: 16 additions & 0 deletions Week3/solutions/hw2_dorzhiev_ardan/hw2_task3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/* Вывести сотрудников, которые старше своего менеджера, но были наняты позже своего менеджера.
* Вывести полное имя сотрудника(Фамилия<Пробел>Имя), день рождения, дата найма, полное имя менеджера, день рождения, дата найма.
* (employee_full_name, employee_birth_date, employee_hire_date, manager_full_name, manager_birth_date, manager_hire_date)
*/

select concat(e.last_name, ' ', e.first_name) as employee_full_name
, e.birth_date as employee_birth_date
, e.hire_date as employee_hire_date
, concat(m.last_name, ' ', m.first_name) as manager_full_name
, m.birth_date as manager_birth_date
, m.hire_date as manager_hire_date
from employee e
join employee m on e.reports_to = m.employee_id
where e.birth_date < m.birth_date -- работник старше своего менеджера
and e.hire_date > m.hire_date -- работник нанят позже менеджера

28 changes: 28 additions & 0 deletions Week3/solutions/hw2_dorzhiev_ardan/hw2_task4.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/* Для всех треков жанра Rock и Metal, для каждого артиста подсчитать количество треков.
* Вывести имя артиста, количество треков. (artist_name, qty)
*/

select ar."name" as artist_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(t.track_id) as qty
from track t
join genre g on t.genre_id = g.genre_id
join album a on t.album_id = a.album_id
join artist ar on a.artist_id = ar.artist_id
where g."name" in ('Rock', 'Metal')
group by ar.artist_id
, ar."name"
order by count(t.track_id)

/*select ar."name"
, g."name"
, t.track_id
, t."name"
, a.album_id
, a.title
from track t
join genre g on t.genre_id = g.genre_id
join album a on t.album_id = a.album_id
join artist ar on a.artist_id = ar.artist_id
where g."name" in ('Rock', 'Metal')
order by ar."name", t.track_id
*/
42 changes: 42 additions & 0 deletions Week3/solutions/hw2_dorzhiev_ardan/hw2_task5.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/* Для каждого сотрудника
* подсчитать количество клиентов,
* количество выполненных заказов,
* количество уникальных адресов доставки
* общее число приобретенных треков
* вывести полное имя сотрудника.
* (employee_full_name, invoices_count, customers_count, addresses_count, tracks_count)
*/

select concat(e.last_name, ' ', e.first_name) as employee_full_name
, count(distinct c.customer_id) as customers_count
, count(distinct i.invoice_id) as invoices_count
, count(distinct i.billing_address) as addresses_count
, count(iline.track_id) as tracks_count
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 employee e
left join customer c on e.employee_id = c.support_rep_id
left join invoice i on i.customer_id = c.customer_id
left join invoice_line iline on i.invoice_id = iline.invoice_id
group by e.employee_id
, concat(e.last_name, ' ', e.first_name)

/*
select *
from invoice i
left join invoice_line iline on i.invoice_id = iline.invoice_id

select e.employee_id
, e.title
, concat(e.last_name, ' ', e.first_name) as employee_full_name
, c.customer_id
, i.invoice_id
, i.billing_address
, iline.track_id
from employee e
left join customer c on e.employee_id = c.support_rep_id
left join invoice i on i.customer_id = c.customer_id
left join invoice_line iline on i.invoice_id = iline.invoice_id
order by e.employee_id */




51 changes: 51 additions & 0 deletions Week3/solutions/hw2_dorzhiev_ardan/hw2_task6.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* Выполнить сегментацию клиентов по количеству букв "а" в его фамилии - Группа 'A' - 0 букв, 'B' - 1 буква, 'C' - 2 буквы, 'D' - 3 и более букв.
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.

Принято

* Для каждой группы вывести количество уникальных типов медиа, купленных клиентами по трекам из различных жанров метала.
* Вывести имя группы и количество уникальных типов медиа (segment_group, cnt)
* */

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'
else 'E' end as segment_group
, count(distinct t.media_type_id) as cnt
from customer c
join invoice i on c.customer_id = i.customer_id
join invoice_line il on il.invoice_id = i.invoice_id
join track t on il.track_id = t.track_id
join genre g on g.genre_id = t.genre_id
where lower(g."name") like '%metal%'
group by 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'
else 'E' end

/*
select c.last_name
, i.invoice_id
, il.invoice_line_id
, il.track_id
, t.media_type_id
, g.genre_id
from customer c
join invoice i on c.customer_id = i.customer_id
join invoice_line il on il.invoice_id = i.invoice_id
join track t on il.track_id = t.track_id
join genre g on g.genre_id = t.genre_id
where lower(g."name") like '%metal%'
order by c.last_name

select t.media_type_id
from customer c
join invoice i on c.customer_id = i.customer_id
join invoice_line il on il.invoice_id = i.invoice_id
join track t on il.track_id = t.track_id
join genre g on g.genre_id = t.genre_id
where lower(g."name") like '%metal%'
group by t.media_type_id


*/


Loading