显示具有相同值的行数

时间:2016-11-05 15:01:19

标签: sql clickhouse

我有简单的表格:

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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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

我刚使用了Combinator“If”。我希望这可以帮到你。