显示具有相同值的行数

holgip5t  于 2021-07-15  发布在  ClickHouse
关注(0)|答案(2)|浏览(543)

我有一张简单的table:

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 ? e、 g.我有疑问:

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

mkh04yzy1#

抱歉,我没法给你魔法。但是,这看起来更好:

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

我刚用了组合词“如果”。我希望这能帮到你。

0ve6wy6x

0ve6wy6x2#

您可以在以下情况下使用case

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

相关问题