ClickHouse 入门语法了解

DDL

建表语句

create table temp.table_name(
sold_time_partition_key Date, 	---默认分区键字段
id Int32,	----integer类型在CK中是默认以Int32为主
user_name String,	---character varying/char 中默认字符类型为String
pass_word Nullable(String), ----Nullable该字段为不为空
net_sale Float32,	----numeric类型默认为Float32
create_time DateTime ----Datatime 为时间戳类型yyyy-MM-dd HH:mi:ss,而Data为yyyy-MM-dd
)ENGINE = MergeTree() PARTITION BY toYYYYMMDD(sold_time_partition_key) ORDER BY id SETTINGS index_granularity = 8192;
----MergeTree为合并数  PARTITION BY 为分区键   ORDER BY 则为排序字段,一般情况默认为id

----删除表操作
drop table temp.table_name;
----情况表操作
truncate table temp.table_name;

若是对CK主键有疑问的,可以参考这位大佬的博客了解下

创建视图

----创建视图
create view temp.v_viem_table as 
select * from temp.table_name;
----删除视图
drop table temp.v_viem_table;

DML

插入操作

insert into temp.table_name_test as 
select * from temp.table_name;

查询操作

select 
	countDistinct(if(is_not_sales = n, trans_code,NULL)) S trans_count,
	round(sum(IF(is_not_sales = n, sale_qty, NULL)), 2) AS sale_qty_a,
from temp.table_name
----countDistinct(***) 该函数等同于count(distinct ***)
----count(***) 该函数等同于count(***)
----sum(***) 该函数等同于sum(***)
----round(***,2) 保留2位小数
----IF(is_not_sales = n, sale_qty, NULL) 该函数等同于 case when is_not_sales = n then sale_qty else NULL end

日期操作

----获取当天时间
select today()  ---2020-06-05
----获取当前时间戳
select now()	---2020-06-05 15:05:38
----获取当前月份的天数
select toInt32(formatDateTime(subtractDays(toStartOfMonth(addMonths(today(),1)),1),%d))	---30

待续。。。。。。。

经验分享 程序员 微信小程序 职场和发展