diff --git a/aysat/lifecoding/SQL1.sql b/aysat/lifecoding/SQL1.sql new file mode 100644 index 0000000..05df519 --- /dev/null +++ b/aysat/lifecoding/SQL1.sql @@ -0,0 +1,45 @@ +''' + Задача 1. + +Таблица users + +| user_id | date | +| --- | --- | +| 1 | 2018-07-01 | +| 234 | 2018-07-02 | +| 3 | 2018-07-02 | +| 1 | 2018-07-02 | +| ... | ... | +| 234 | 2018-10-04 | + +Нужно найти ежемесячное процентное изменение месячной аудитории активных пользователей. +''' + +with monthly_users as ( + select + count(distinct(user_id)) as active_users, + date_trunc('month', date) as month + from + abakhytbekuly.users + group by + date_trunc('month', date) +), + change as ( + select active_users, + month, + lag(active_users) over(order by month) as prev_mau + from + monthly_users +) +select + active_users, + month, + prev_mau, + 100.0 * (active_users - prev_mau) / prev_mau as pct_change -- тут результат странный типа '-66.666666' +from + change +where + prev_mau is not null +order by + month + diff --git a/aysat/lifecoding/SQL2.sql b/aysat/lifecoding/SQL2.sql new file mode 100644 index 0000000..78c58ec --- /dev/null +++ b/aysat/lifecoding/SQL2.sql @@ -0,0 +1,48 @@ +''' +Требуется написать SQL-запрос, который будет находить ошибки в версионности по ключу employee_id, такие как «дыры» в истории и пересечение версий. +Результатом запроса является список пар employee_id и from_dt, в которых есть ошибка. +Для каждой пары вывести тип ошибки. + +Salary + +Employee_id Salary_amt From_dt To_dt +1 50 000.000 01.01.2019 01.02.2019 +1 70 000.000 01.04.2019 01.01.9999 +2 70 000.000 01.01.2019 01.05.2019 +2 90 000.000 01.03.2019 01.01.9999 +3 45 000.000 01.01.2019 01.01.9999 + +''' + +with base as ( + select + employee_id, + to_dt, + from_dt, + lag(to_dt) over (partition by employee_id order by from_dt) as lag_to_dt +from + abakhytbekuly.salary +), +error as ( + select + employee_id, + from_dt, + case when + lag_to_dt > from_dt then 'пересечение' + when + lag_to_dt < from_dt then 'дыра' + end + as status + from + base + where + lag_to_dt is not null +) +select +* +from + error +order by + employee_id, from_dt + + \ No newline at end of file