我是 clickhouse 或任何柱状数据库的新手。我需要像在 sql-server、postgres 或任何其他基于行的数据库中那样对表进行透视。
我正在寻找一个通用的解决方案,但是这里示例的解决方案会很好。
表:存储
Tag Slot Reading
--- ---- --------
A 1 5
B 1 6
C 1 1
A 2 2
B 2 8
C 3 2
.
.
millions of rows
转置为:
Slot A B C and so on
--- -- -- --
1 5 6 1
2 2 8 -
3 - - 2
.
.
and so on
标签可以是 100 到 1000 之间的任意位置,插槽可以是 1000-10000 之间。不过没关系。
我只需要使用 sql 来完成此操作。
谢谢。
最佳答案
create table xxx (Tag String, Slot Int64, Reading Int64) Engine=Memory;
insert into xxx values
('A',1,5),
('B',1,6),
('C',1,1),
('A',2,2),
('B',2,8),
('C',3,2)
SELECT
Slot,
groupArray((Tag, Reading))
FROM xxx
GROUP BY Slot
┌─Slot─┬─groupArray(tuple(Tag, Reading))─┐
│ 3 │ [('C',2)] │
│ 2 │ [('A',2),('B',8)] │
│ 1 │ [('A',5),('B',6),('C',1)] │
└──────┴─────────────────────────────────┘
关于pivot-table - Pivot 或 clickhouse 中的同等功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61781720/