注意:要实现完整的行转列还是得用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;
Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐