我是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就能做到这一点。
谢谢。
答案 0 :(得分:0)
select Slot,
sumIf(Reading, Tag='A') A,
sumIf(Reading, Tag='B') B,
...
group by Slot
select Slot, arrayReduce('sumMap', [(groupArray(tuple(Tag,Reading)) as a).1], [a.2])
...
group by Slot
select Slot, groupArray(tuple(Tag, Reading))
from
(select Slot, Tag, sum(Reading) Reading
...
group by Slot, Tag)
group by Slot
答案 1 :(得分:0)
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)] │
└──────┴─────────────────────────────────┘