Merge pull request '300k-nanosek' (#16) from test-Pidr into main

Reviewed-on: https://gitea.de-infra.servehttp.com/de-infrastructure/DWH/pulls/16
Reviewed-by: aleksey bogatyrev <abogatyrev@example.com>
This commit is contained in:
2025-12-03 17:04:11 +04:00
2 changed files with 93 additions and 0 deletions
+45
View File
@@ -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
+48
View File
@@ -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