【问题标题】:include "0" count result in clickhouse在 clickhouse 中包含“0”计数结果
【发布时间】:2020-10-24 22:26:16
【问题描述】:

我有一个 t_table(some_date, id),我想获取两个输入日期之间每天的 id 计数。

我试试这个查询:

SELECT dateDiff('day', toDateTime('2020-07-01 07:34:22'), some_date) as day,
       count(distinct id) as ids,
from t_table
where (some_date between '2020-07-01 00:09:25' and '2020-07-15 21:09:25') group by day order by day;

仅当 id 具有 (some_date) 的这一天时,此查询才会返回日期。我想获取输入日期之间所有日期的列,如果在 ids 列中的某一天没有 id,则将 0 放入该单元格。

我认为需要使用一些连接,但我不知道使用什么连接。

【问题讨论】:

    标签: sql clickhouse


    【解决方案1】:

    按...排序

    SELECT dateDiff('day', toDateTime('2020-07-01 07:34:22'), some_date) as day,
           count() as ids
    from (select toDateTime(arrayJoin(['2020-07-02 00:09:25','2020-07-02 00:10:25','2020-07-15 00:00:25'])) some_date)
    where (some_date between '2020-07-01 00:09:25' and '2020-07-15 21:09:25') group by day order by day
    ┌─day─┬─ids─┐
    │   1 │   2 │
    │  14 │   1 │
    └─────┴─────┘
    
    
    SELECT dateDiff('day', toDateTime('2020-07-01 07:34:22'), some_date) as day,
           count() as ids
    from (select toDateTime(arrayJoin(['2020-07-02 00:09:25','2020-07-02 00:10:25','2020-07-15 00:00:25'])) some_date)
    where (some_date between '2020-07-01 00:09:25' and '2020-07-15 21:09:25') group by day order by day
    with fill;
    ┌─day─┬─ids─┐
    │   1 │   2 │
    │   2 │   0 │
    │   3 │   0 │
    │   4 │   0 │
    │   5 │   0 │
    │   6 │   0 │
    │   7 │   0 │
    │   8 │   0 │
    │   9 │   0 │
    │  10 │   0 │
    │  11 │   0 │
    │  12 │   0 │
    │  13 │   0 │
    │  14 │   1 │
    └─────┴─────┘
    

    【讨论】:

    • 非常感谢,这正是我要找的
    【解决方案2】:

    您可以使用numbers 函数生成数字,然后将它们转换为日期。如果我遵循逻辑,则查询的其余部分将遵循 left join 聚合:

    with dates as (
          select toDateTime('2020-07-01 00:09:25') + n as dt, number as n
          from numbers(1, 15)
         )
    select d.n as day, count(distinct t.id) as ids,
    from dates d left join
         t_table t
         on t.some_date >= d.dt and
            t.some_date < d.dt + 1
    group by day
    order by day;
    

    我不能 100% 确定 Clickhouse 是否接受 LEFT JOIN 上的这些条件。你可能不得不使用这样的东西:

    on toDate(some_date) = to_date(d.dt)
    

    【讨论】:

    • 带有语句返回错误,我必须使用with (select...) as dates。而且我不能在加入时使用日期。 DB 返回异常:e.displayText() = DB::Exception: 表 test.dates 不存在。 (版本 20.5.2.7(正式版))
    【解决方案3】:

    试试这个查询:

    SELECT
        greatest(day, date_range.day) AS day,
        ids
    FROM
    (
        SELECT *
        FROM
        (
            SELECT
                dateDiff('day', toDateTime('2020-07-01 07:34:22'), some_date) AS day,
                countDistinct(id) AS ids
            FROM t_table
            WHERE (some_date >= '2020-07-01 00:09:25') AND (some_date <= '2020-07-15 21:09:25')
            GROUP BY day
        ) AS result
        FULL OUTER JOIN
        (
            SELECT toDate('2020-07-01 00:09:25') + number AS day
            FROM numbers(0, 15)
        ) AS date_range ON result.day = date_range.day
    )
    ORDER BY date ASC
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-11
      • 2017-12-11
      • 2013-05-07
      • 1970-01-01
      • 1970-01-01
      • 2020-08-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多