clickhouse-行列转换
·
参考文章1:ck
参考文章2:其他sql
参考文章3: ck ,不一定有用
注意:要实现完整的行转列还是得用pivot;
不得已的方法可以采用case when,sumif,maxif等;
准备数据:
DROP TABLE zhuzh_test.tabel
create table if not exists zhuzh_test.tabel
(
store String,
sku Nullable(String),
rank UInt8
)
engine = MergeTree ORDER BY store
SETTINGS index_granularity = 8192;
INSERT INTO zhuzh_test.tabel (store, sku, rank) VALUES ('A', '18-01', 1);
INSERT INTO zhuzh_test.tabel (store, sku, rank) VALUES ('A', '18-09', 2);
INSERT INTO zhuzh_test.tabel (store, sku, rank) VALUES ('A', '18-11', 3);
INSERT INTO zhuzh_test.tabel (store, sku, rank) VALUES ('B', '18-33', 1);
INSERT INTO zhuzh_test.tabel (store, sku, rank) VALUES ('B', '18-01', 2);
编写sql:
select
store,
maxIf(sku, rank = 1) AS sku_1,
maxIf(sku, rank = 2) AS sku_2,
maxIf(sku, rank = 3) AS sku_3
from tabel
group by store
order by store;
更多推荐
所有评论(0)