136 lines
4.2 KiB
SQL
136 lines
4.2 KiB
SQL
CREATE TABLE mdrozdov.users (
|
|
user_id Int,
|
|
name String,
|
|
age Int,
|
|
country String,
|
|
is_active bool
|
|
)
|
|
engine = MergeTree
|
|
order by user_id
|
|
|
|
insert into mdrozdov.users values
|
|
(1, 'Ivan' ,30, 'RU', 1),
|
|
(2, 'Anna' ,25, 'RU', 1),
|
|
(3, 'Petr' ,45, 'RU', 0),
|
|
(4, 'Olga' ,35, 'RU', 1),
|
|
(5, 'John' ,55, 'US', 1),
|
|
(6, 'Alice' ,42, 'US', 0),
|
|
(7, 'Tom' ,38, 'US', 1),
|
|
(8, 'Lucas' ,28, 'BR', 1),
|
|
(9, 'Maria' ,60, 'BR', 1),
|
|
(10, 'João' ,27, 'BR', 0),
|
|
(11, 'Kenji' ,33, 'JP', 1),
|
|
(12, 'Yuki' ,29, 'JP', 0),
|
|
(13, 'Chen' ,40, 'CN', 1),
|
|
(14, 'Li' ,48, 'CN', 1),
|
|
(15, 'Fatima',31, 'AE', 1),
|
|
(16, 'Ahmed' ,36, 'AE', 1),
|
|
(17, 'George',24, 'US', 1),
|
|
(18, 'Nina' ,34, 'RU', 1),
|
|
(19, 'Max' ,50, 'DE', 1),
|
|
(20, 'Sophie',22, 'DE', 0)
|
|
|
|
--1. Получи 10 самых старших **активных** пользователей.
|
|
select
|
|
user_id,
|
|
name,
|
|
age,
|
|
country,
|
|
is_active
|
|
from mdrozdov.users
|
|
where is_active
|
|
order by age desc
|
|
limit 10
|
|
--2. Выведи пользователей из `country = 'RU'`, отсортированных по имени.
|
|
select
|
|
user_id,
|
|
name,
|
|
age,
|
|
country,
|
|
is_active
|
|
from mdrozdov.users
|
|
where country = 'RU'
|
|
order by name asc
|
|
--3. Что произойдёт, если написать `LIMIT 10` **без `ORDER BY`**?
|
|
-- ответ будет недетерминирован
|
|
--4. Посчитай количество пользователей по странам.
|
|
select
|
|
country,
|
|
count(user_id) as total_users
|
|
from mdrozdov.users
|
|
group by country
|
|
--5. Для каждой страны выведи минимальный и максимальный возраст.
|
|
select
|
|
country,
|
|
min(age) as min_user_age,
|
|
max(age) as max_user_age
|
|
from mdrozdov.users
|
|
group by country
|
|
--6. Отсортируй страны по убыванию количества пользователей.
|
|
select
|
|
country,
|
|
count(user_id) as total_users
|
|
from mdrozdov.users
|
|
group by country
|
|
order by total_users desc
|
|
|
|
-- task 2
|
|
CREATE TABLE mdrozdov.events (
|
|
user_id Int,
|
|
event_time DateTime,
|
|
event_type String --mb better enum but idc
|
|
)
|
|
engine = MergeTree
|
|
order by user_id
|
|
insert into events values
|
|
(1, '2025-07-17 09:00:00', 'click'),
|
|
(1, '2025-07-17 09:05:00', 'view'),
|
|
(1, '2025-07-17 09:10:00', 'purchase'),
|
|
(2, '2025-07-17 10:00:00', 'click'),
|
|
(2, '2025-07-17 10:05:00', 'click'),
|
|
(2, '2025-07-17 11:00:00', 'view'),
|
|
(3, '2025-07-16 12:00:00', 'view'),
|
|
(3, '2025-07-16 12:30:00', 'click'),
|
|
(3, '2025-07-17 08:00:00', 'purchase'),
|
|
(4, '2025-07-17 13:00:00', 'click'),
|
|
(4, '2025-07-17 14:00:00', 'view'),
|
|
(4, '2025-07-17 15:00:00', 'view'),
|
|
(5, '2025-07-16 20:00:00', 'view'),
|
|
(5, '2025-07-17 01:00:00', 'click'),
|
|
(5, '2025-07-17 06:00:00', 'purchase'),
|
|
(6, '2025-07-17 07:30:00', 'view'),
|
|
(6, '2025-07-17 08:45:00', 'click'),
|
|
(7, '2025-07-16 23:59:59', 'click'),
|
|
(7, '2025-07-17 00:01:00', 'purchase'),
|
|
(7, '2025-07-17 10:10:00', 'view')
|
|
--1-2) Добавь колонку с кумулятивным количеством событий по user_id (используй count() OVER ...)
|
|
select
|
|
user_id,
|
|
event_time,
|
|
event_type,
|
|
count(*) over (partition by user_id order by event_time) as total_click_exact_moment,
|
|
row_number() over (partition by user_id order by event_time) as row_number
|
|
from mdrozdov.events
|
|
--3) да, поддерживается, но не обязательно для коммулятивного суммирования строк
|
|
--4-6) Сделай WITH recent_events AS (...) — все события за последние 24 часа.
|
|
with mdrozdov.recent_events AS (
|
|
select *
|
|
from mdrozdov.events
|
|
where event_time >= toDateTime('2025-07-18') - interval 1 day
|
|
), category_list as (
|
|
select 'click' as event_type, 'interaction' as category
|
|
union all
|
|
select 'view', 'interaction'
|
|
union all
|
|
select 'purchase', 'conversion'
|
|
)
|
|
select
|
|
user_id,
|
|
count(*) as total_events_last_24h,
|
|
count(distinct cl.category) as uniq_categories
|
|
from recent_events re
|
|
left join category_list cl
|
|
using(event_type)
|
|
group by user_id
|
|
order by total_events_last_24h desc, user_id asc
|
|
limit 5 |