From 797a5b19692a91245451795434f6ac65270cb31f Mon Sep 17 00:00:00 2001 From: Aysat Date: Tue, 25 Nov 2025 22:58:04 +0600 Subject: [PATCH 1/9] test clickhouse code --- aysat/clickhouse/TESTcode_blya.sql | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 aysat/clickhouse/TESTcode_blya.sql diff --git a/aysat/clickhouse/TESTcode_blya.sql b/aysat/clickhouse/TESTcode_blya.sql new file mode 100644 index 0000000..36d431e --- /dev/null +++ b/aysat/clickhouse/TESTcode_blya.sql @@ -0,0 +1,21 @@ +CREATE MATERIALIZED VIEW lesson_popularity_summary +ENGINE = SummingMergeTree() +ORDER BY (course_id, lesson_id) AS +SELECT + lv.lesson_id as lesson_id, + l.title as lesson_title, + lv.course_id as course_id, + c.title as course_title, + count() as total_views, + uniq(lv.user_id) as unique_users, + min(lv.viewed_at) as first_view, + max(lv.viewed_at) as last_view +FROM fact_lesson_views lv +LEFT JOIN dim_lesson l ON lv.lesson_id = l.lesson_id +LEFT JOIN dim_course c ON lv.course_id = c.course_id +GROUP BY + lv.lesson_id, + l.title, + lv.course_id, + c.title; +SELECT * FROM lesson_popularity_summary; \ No newline at end of file From a6c0cde36c76f98430e99895401271ead76fa4a4 Mon Sep 17 00:00:00 2001 From: Aysat Date: Tue, 25 Nov 2025 23:08:31 +0600 Subject: [PATCH 2/9] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B8=D0=BB=20sele?= =?UTF-8?q?ct=20=D0=B7=D0=B0=D0=BF=D1=80=D0=BE=D1=81=20=D0=B8=20=D1=87?= =?UTF-8?q?=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aysat/clickhouse/TESTcode_blya.sql | 1 - 1 file changed, 1 deletion(-) diff --git a/aysat/clickhouse/TESTcode_blya.sql b/aysat/clickhouse/TESTcode_blya.sql index 36d431e..9f18ae3 100644 --- a/aysat/clickhouse/TESTcode_blya.sql +++ b/aysat/clickhouse/TESTcode_blya.sql @@ -18,4 +18,3 @@ GROUP BY l.title, lv.course_id, c.title; -SELECT * FROM lesson_popularity_summary; \ No newline at end of file From 0d12df957281f56648122b8defbb905626482202 Mon Sep 17 00:00:00 2001 From: Aysat Date: Wed, 26 Nov 2025 12:11:48 +0600 Subject: [PATCH 3/9] =?UTF-8?q?=D0=AD=D1=82=D0=BE=201=20=D0=B7=D0=B0=D0=B4?= =?UTF-8?q?=D0=B0=D1=87=D0=B0=20=D0=BD=D0=B0=20sql,=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D1=82=D0=BE=D1=82=20=D0=B2=D1=8B=D0=B2=D0=BE=D0=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aysat/lifecoding/SQL1.sql | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 aysat/lifecoding/SQL1.sql diff --git a/aysat/lifecoding/SQL1.sql b/aysat/lifecoding/SQL1.sql new file mode 100644 index 0000000..667bc05 --- /dev/null +++ b/aysat/lifecoding/SQL1.sql @@ -0,0 +1,19 @@ +with cte1 as ( +select count(distinct(user_id)) as mau, +date_trunc('month', date) as month +from abakhytbekuly.users +group by date_trunc('month', date) +), +cte2 as ( +select mau, +month, +lag(mau) over(order by month) as prev_mau +from cte1 +) +select mau, +month, +prev_mau, +100.0 * (mau - prev_mau) / prev_mau as pct_change +from cte2 +order by month + From 1e2adfc3e94d510fb6dac369ee79b58149827173 Mon Sep 17 00:00:00 2001 From: Aysat Date: Wed, 26 Nov 2025 12:20:41 +0600 Subject: [PATCH 4/9] =?UTF-8?q?1=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BF=D0=BE=20SQL,=20=D1=80=D0=B5=D0=B7=D1=83=D0=BB?= =?UTF-8?q?=D1=8C=D1=82=D0=B0=D1=82=20=D0=BD=D0=B5=20=D1=82=D0=BE=D1=82,?= =?UTF-8?q?=20=D0=BF=D1=80=D0=BE=D0=B2=D0=B5=D1=80=D1=8C=20=D0=BF=D0=B6=20?= =?UTF-8?q?=D0=BF=D0=B6=20=D0=BF=D0=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aysat/lifecoding/SQL1.sql | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/aysat/lifecoding/SQL1.sql b/aysat/lifecoding/SQL1.sql index 667bc05..75ac5d3 100644 --- a/aysat/lifecoding/SQL1.sql +++ b/aysat/lifecoding/SQL1.sql @@ -1,3 +1,20 @@ +''' + Задача 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 cte1 as ( select count(distinct(user_id)) as mau, date_trunc('month', date) as month From 74ba96eb98e76e67eb40dee285312e1fae0428da Mon Sep 17 00:00:00 2001 From: Aysat Date: Wed, 26 Nov 2025 14:53:10 +0600 Subject: [PATCH 5/9] =?UTF-8?q?2=20=D0=B7=D0=B0=D0=B4=D0=B0=D1=87=D0=BA?= =?UTF-8?q?=D0=B0=20=D0=BF=D0=BE=20=D0=A1=D0=9A=D0=9B.=20=D0=92=D1=80?= =?UTF-8?q?=D0=BE=D0=B4=D0=B5=20=D0=BD=D0=BE=D1=80=D0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aysat/lifecoding/SQL2.sql | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 aysat/lifecoding/SQL2.sql diff --git a/aysat/lifecoding/SQL2.sql b/aysat/lifecoding/SQL2.sql new file mode 100644 index 0000000..96c4aeb --- /dev/null +++ b/aysat/lifecoding/SQL2.sql @@ -0,0 +1,23 @@ +with base as ( +select +employee_id, +to_dt, +from_dt, +lag(to_dt) over (partition by employee_id order by from_dt) 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 From 661bf96688e4f6fa0fedad4c149a30a50418fc64 Mon Sep 17 00:00:00 2001 From: Aysat Date: Wed, 26 Nov 2025 15:23:40 +0600 Subject: [PATCH 6/9] =?UTF-8?q?=D0=9F=D0=BE=D0=BC=D0=B5=D0=BD=D1=8F=D0=BB?= =?UTF-8?q?=20=D1=84=D0=BE=D1=80=D0=BC=D1=83=20=D0=B8=20=D0=BD=D0=B0=D0=B7?= =?UTF-8?q?=D0=B2=D0=B0=D0=BD=D0=B8=D1=8F=20=D0=B8=20=D0=B8=D1=81=D0=BF?= =?UTF-8?q?=D1=80=D0=B0=D0=B2=D0=B8=D0=BB=20NULL?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aysat/lifecoding/SQL1.sql | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/aysat/lifecoding/SQL1.sql b/aysat/lifecoding/SQL1.sql index 75ac5d3..8af1809 100644 --- a/aysat/lifecoding/SQL1.sql +++ b/aysat/lifecoding/SQL1.sql @@ -15,22 +15,23 @@ Нужно найти ежемесячное процентное изменение месячной аудитории активных пользователей. ''' -with cte1 as ( -select count(distinct(user_id)) as mau, +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) ), -cte2 as ( -select mau, +change as ( +select active_users, month, -lag(mau) over(order by month) as prev_mau -from cte1 +lag(active_users) over(order by month) as prev_mau +from monthly_users ) -select mau, +select active_users, month, prev_mau, -100.0 * (mau - prev_mau) / prev_mau as pct_change -from cte2 +100.0 * (active_users - prev_mau) / prev_mau as pct_change -- тут результат странный типа '-66.666666' +from change +where prev_mau is not null order by month From 47fa72c7577654c5ca92d6c64143eae1edc3c977 Mon Sep 17 00:00:00 2001 From: Aysat Date: Wed, 26 Nov 2025 15:45:18 +0600 Subject: [PATCH 7/9] =?UTF-8?q?=D0=9F=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=84=D0=BE=D1=80=D0=BC=D1=83=20=D0=B8=20=D0=B4=D0=BE?= =?UTF-8?q?=D0=B1=D0=B0=D0=B2=D0=B8=D0=BB=20=D0=BE=D0=BF=D0=B8=D1=81=D0=B0?= =?UTF-8?q?=D0=BD=D0=B8=D0=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aysat/lifecoding/SQL1.sql | 40 +++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/aysat/lifecoding/SQL1.sql b/aysat/lifecoding/SQL1.sql index 8af1809..05df519 100644 --- a/aysat/lifecoding/SQL1.sql +++ b/aysat/lifecoding/SQL1.sql @@ -16,22 +16,30 @@ ''' 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) + 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 + 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 +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 From 3419651d1211aa3c32376f0f2d184f5e5e45e11e Mon Sep 17 00:00:00 2001 From: Aysat Date: Wed, 26 Nov 2025 19:27:34 +0600 Subject: [PATCH 8/9] =?UTF-8?q?=D0=BF=D0=BE=D0=BF=D1=80=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D1=87=D0=B8=D1=82=D0=B0=D0=B5=D0=BC=D0=BE=D1=81=D1=82?= =?UTF-8?q?=D1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aysat/lifecoding/SQL2.sql | 63 +++++++++++++++++++++++++++------------ 1 file changed, 44 insertions(+), 19 deletions(-) diff --git a/aysat/lifecoding/SQL2.sql b/aysat/lifecoding/SQL2.sql index 96c4aeb..78c58ec 100644 --- a/aysat/lifecoding/SQL2.sql +++ b/aysat/lifecoding/SQL2.sql @@ -1,23 +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) lag_to_dt -from abakhytbekuly.salary + 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 + 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 +select +* +from + error +order by + employee_id, from_dt + + \ No newline at end of file From 0459718ae3a34b5785707af4753ee94b6306649f Mon Sep 17 00:00:00 2001 From: Aysat Date: Tue, 2 Dec 2025 13:59:22 +0600 Subject: [PATCH 9/9] =?UTF-8?q?=D1=83=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- aysat/clickhouse/TESTcode_blya.sql | 20 -------------------- 1 file changed, 20 deletions(-) delete mode 100644 aysat/clickhouse/TESTcode_blya.sql diff --git a/aysat/clickhouse/TESTcode_blya.sql b/aysat/clickhouse/TESTcode_blya.sql deleted file mode 100644 index 9f18ae3..0000000 --- a/aysat/clickhouse/TESTcode_blya.sql +++ /dev/null @@ -1,20 +0,0 @@ -CREATE MATERIALIZED VIEW lesson_popularity_summary -ENGINE = SummingMergeTree() -ORDER BY (course_id, lesson_id) AS -SELECT - lv.lesson_id as lesson_id, - l.title as lesson_title, - lv.course_id as course_id, - c.title as course_title, - count() as total_views, - uniq(lv.user_id) as unique_users, - min(lv.viewed_at) as first_view, - max(lv.viewed_at) as last_view -FROM fact_lesson_views lv -LEFT JOIN dim_lesson l ON lv.lesson_id = l.lesson_id -LEFT JOIN dim_course c ON lv.course_id = c.course_id -GROUP BY - lv.lesson_id, - l.title, - lv.course_id, - c.title;