我有简单的表:
CREATE TABLE clicks
(
Year UInt16,
Week UInt8,
Day UInt8,
Month UInt8,
ClickDate Date,
ClickTime DateTime,
AdvertId String,
UserId String,
Age UInt8,
Country FixedString(2),
Gender Enum8('male' = 1, 'female' = 2),
Ip String
)
ENGINE = MergeTree(ClickDate, (Year, ClickDate), 8192);
是否可以为一列添加一个唯一值的行总和而不重复值并将它们分组ClickDate
?例如我有查询:
SELECT
count() AS Summary,
ClickDate,
SUM(roundAge(Age) = 17) AS Age_17,
SUM(roundAge(Age) = 25) AS Age_25,
SUM(roundAge(Age) = 35) AS Age_35,
SUM(roundAge(Age) = 45) AS Age_45,
SUM(Age = 0) AS Age_empty
FROM clicks
GROUP BY ClickDate
在这种情况下,我需要复制每个Age
值
SUM(roundAge(Age) = 17) AS Age_17,
SUM(roundAge(Age) = 25) AS Age_25
我怎样才能摆脱这种重复?我的期望:
源数据:
┌──ClickDate─┬─RoundedAge─┐
│ 2016-10-09 │ 17 │
└────────────┴────────────┘
┌──ClickDate─┬─RoundedAge─┐
│ 2016-10-09 │ 25 │
│ 2016-10-09 │ 17 │
│ 2016-10-09 │ 45 │
│ 2016-10-09 │ 45 │
│ 2016-10-09 │ 35 │
│ 2016-10-09 │ 45 │
│ 2016-10-09 │ 25 │
│ 2016-10-09 │ 18 │
│ 2016-10-10 │ 25 │
│ 2016-10-10 │ 25 │
└────────────┴────────────┘
┌──ClickDate─┬─RoundedAge─┐
│ 2016-10-09 │ 17 │
└────────────┴────────────┘
所需输出:
┌─Summary─┬──ClickDate─┬─Age_17─┬─Age_25─┬─Age_35─┬─Age_45─┬─Age_empty─┐
│ 10 │ 2016-10-09 │ 3 │ 2 │ 1 │ 3 │ 0 │
│ 2 │ 2016-10-10 │ 0 │ 2 │ 0 │ 0 │ 0 │
└─────────┴────────────┴────────┴────────┴────────┴────────┴───────────┘