ClickHouse中的SQL語法
阿新 • • 發佈:2020-12-07
– with語法
with '1' as v select * from tb_user where id=v;
--求平均年齡
with (select count(1) from tb_user) as cnt select (sum(age)/cnt) from tb_user;
array join 語法
array join 相當於explode+lateral view
select id,arr from tb_arr_join array join arr; --會覆蓋原先的陣列
select id,arr, x from tb_arr_join array join arr as x ;
select id,arr,x from tb_arr_join array join [1,2,3,4,5] as x; --自動拼
┌─id─┬─arr──────────────┬─x─┐
│ 1 │ ['a1','a2'] │ 1 │
│ 1 │ ['a1','a2'] │ 2 │
│ 1 │ ['a1','a2'] │ 3 │
│ 1 │ ['a1','a2'] │ 4 │
│ 1 │ ['a1','a2'] │ 5 │
│ 2 │ [ 'b1','b2','b3'] │ 1 │
│ 2 │ ['b1','b2','b3'] │ 2 │
│ 2 │ ['b1','b2','b3'] │ 3 │
│ 2 │ ['b1','b2','b3'] │ 4 │
│ 2 │ ['b1','b2','b3'] │ 5 │
└────┴──────────────────┴───┘
案例需求:將如下資料成功轉換
程式碼實現:
select id ,e,i from (select id ,groupArray(name) arr,
arrayEnumerate(arr) arr_index
from tb_test_arr
group by id ) t
array join arr as e,
arr_index as i
;
FORMAT 指定輸出和輸入的資料格式
clickhouse-client -q "select * from tb_user FORMAT XML"
LIMIT/LIMIT BY 語法
select * from tb_user limit 2,2 ; --從索引為第二個開始找兩個
程式碼實現:
select * from tb_limit limit 2 by name; --通過name分組
create view v_limit as select * from tb_limit ; --建立一個檢視臨時儲存資料 不會儲存到磁碟
create table t_limit engine=Log as select * from tb_limit ;
create table tb_limit2 like tb_limit ; --不支援
建立分割槽表
按照指定欄位分組
create table tb_p(
oid String ,
money Float64 ,
cDate Date
) engine = MergeTree
order by oid
partition by cDate ;
insert into tb_p values ('002',99,'2020-12-01') ,('001',98,'2020-12-01') ,('003',199,'2020-12-02');
┌─oid─┬─money─┬──────cDate─┐
│ 003 │ 199 │ 2020-12-02 │
└─────┴───────┴────────────┘
┌─oid─┬─money─┬──────cDate─┐
│ 001 │ 98 │ 2020-12-01 │
│ 002 │ 99 │ 2020-12-01 │
----------------------------
按照月份分組
create table tb_p2(
oid String ,
money Float64 ,
cDate Date
) engine = MergeTree
order by oid
partition by toMonth(cDate) ;
insert into tb_p2 values ('002',99,'2020-12-01') ,('001',98,'2020-12-01') ,('003',199,'2020-12-02'),('004',299,'2020-11-02');
┌─oid─┬─money─┬──────cDate─┐
│ 001 │ 98 │ 2020-12-01 │
│ 002 │ 99 │ 2020-12-01 │
│ 003 │ 199 │ 2020-12-02 │
└─────┴───────┴────────────┘
┌─oid─┬─money─┬──────cDate─┐
│ 004 │ 299 │ 2020-11-02 │
└─────┴───────┴────────────┘
指定兩個分割槽欄位 年+月 不同就分割槽!!!!
create table tb_p3(
oid String ,
money Float64 ,
cDate Date
) engine = MergeTree
order by oid
partition by (toYear(cDate) , toMonth(cDate)) ;-- 月進行分割槽
insert into tb_p3 values ('002',99,'2020-12-01') ,('001',98,'2020-12-01') ,('003',199,'2020-12-02'),('004',299,'2020-11-02'),('005',299,'2019-11-02')
;
drwxr-x---. 2 clickhouse clickhouse 228 Dec 6 04:33 2019-11_3_3_0
drwxr-x---. 2 clickhouse clickhouse 228 Dec 6 04:33 2020-11_2_2_0
drwxr-x---. 2 clickhouse clickhouse 228 Dec 6 04:33 2020-12_1_1_0