1

我有简单的表:

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 │
└─────────┴────────────┴────────┴────────┴────────┴────────┴───────────┘
4

2 回答 2

2

对不起,我不能为你提供魔法。但是,这看起来更好:

SELECT 
    count() AS Summary, 
    ClickDate, 
    countIf(Age = 17), 
    countIf(Age = 25), 
    countIf(Age = 35), 
    countIf(Age = 45), 
    SUM(Age = 0) AS Age_empty
FROM clicks 
GROUP BY ClickDate

我刚刚使用了组合器“如果”。我希望这可以帮助你。

于 2017-03-07T09:44:49.453 回答
0

您可以使用 CASE WHEN

  SELECT 
  count(*) AS Summary, 
  ClickDate, 
  sum( case when age = 17 then 1 else 0 end),  Age_17,
  sum( case when age = 25 then 1 else 0 end),  Age_25,   
  sum( case when age = 35 then 1 else 0 end),  Age_35,
  sum( case when age = 45 then 1 else 0 end),  Age_45,   
  sum( case when ifnull(age,0) = 0 then 1 else 0 end)  Age_Empty       
  FROM clicks 
  GROUP BY ClickDate

如果你需要范围,你可以在

  SELECT 
  count(*) AS Summary, 
  ClickDate, 
  sum( case when age between 1  and 17 then 1 else 0 end),  Age_17,
  sum( case when age between 18 and 25 then 1 else 0 end),  Age_25,   
  sum( case when age between 26 and 35 then 1 else 0 end),  Age_35,
  sum( case when age between 36 and 45 then 1 else 0 end),  Age_45,   
  sum( case when ifnull(age,0) = 0 then 1 else 0 end)  Age_Empty       
  FROM clicks 
  GROUP BY ClickDate
于 2016-11-05T15:10:31.570 回答