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主键有疑问的,可以参考这位大佬的博客了解下ClickHouse主键探讨[译文+补充]

创建视图

----创建视图
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

待续。。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值