Files
homework/mdrozdov/clickhouse/joins.sql
T
2026-01-31 03:29:26 +07:00

103 lines
1.9 KiB
SQL

--task 1
create table mdrozdov.products (
product_id Int,
name String
)
engine = MergeTree
order by product_id
insert into mdrozdov.products values
(101, 'iPhone 14'),
(102, 'Galaxy S22'),
(103, 'Pixel 7')
create table mdrozdov.prices (
product_id Int,
price Int,
region String
)
engine = MergeTree
order by product_id
insert into mdrozdov.prices values
(101, 999, 'US'),
(101, 1049, 'EU'),
(102, 850, 'US'),
(102, 899, 'CA'),
(103, 799, 'JP')
-- 1)
select
product_id,
name,
price,
region
from mdrozdov.products
any left join mdrozdov.prices
on mdrozdov.products.product_id = mdrozdov.prices.product_id
-- 2)
create table mdrozdov.ticks
ON CLUSTER clickhouse_cluster (
time DateTime,
symbol String,
price Decimal(10, 2)
)
engine = ReplicatedMergeTree(
'/clickhouse/tables/{shard}/mdrozdov/ticks',
'{replica}'
)
order by (time, symbol)
insert into mdrozdov.ticks values
('2025-07-17 10:00:00', 'AAPL', 190.5),
('2025-07-17 10:01:00', 'AAPL', 191.2),
('2025-07-17 10:02:30', 'AAPL', 192.0),
('2025-07-17 10:05:00', 'GOOG', 2850.0)
create table mdrozdov.orders
ON CLUSTER clickhouse_cluster (
time DateTime,
symbol String,
size Int
)
engine = ReplicatedMergeTree(
'/clickhouse/tables/{shard}/mdrozdov/orders',
'{replica}'
)
order by (symbol, time)
insert into mdrozdov.orders values
('2025-07-17 10:00:30', 'AAPL', 50),
('2025-07-17 10:02:00', 'AAPL', 75),
('2025-07-17 10:03:00', 'AAPL', 20),
('2025-07-17 10:04:00', 'GOOG', 15)
select
o.time,
o.symbol,
t.price as last_price
from mdrozdov.orders o
asof join mdrozdov.ticks t
on o.symbol = t.symbol and o.time >= t.time
-- task 3
select
user_id,
name
from users
where user_id in (
select user_id
from payments
where amount > 1000
)
-- task 4
select
bl.user_id,
u.name,
bl.event_time,
bl.event_type
from big_logs bl
any left join users u
using(user_id)