每周而不是每天显示 COUNT(*)

Display COUNT(*) for every week instead of every day

假设我有一个 table,user_id 类型为 Int32login_timeDateTime,格式为 UTC
user_id 不是唯一的,所以 SELECT user_id, login_time FROM some_table; 给出以下结果:

┌─user_id─┬──login_time─┐
│    1    │  2021-03-01 │
│    1    │  2021-03-01 │
│    1    │  2021-03-02 │
│    2    │  2021-03-02 │
│    2    │  2021-03-03 │
└─────────┴─────────────┘

如果我 运行 SELECT COUNT(*) as count, toDate(login_time) as l FROM some_table GROUP BY l 我得到以下结果:

┌─count───┬──login_time─┐
│    2    │  2021-03-01 │
│    2    │  2021-03-02 │
│    1    │  2021-03-03 │
└─────────┴─────────────┘

我想重新格式化结果以每周显示 COUNT,而不是像现在这样每天显示。
上述示例的结果可能如下所示:

┌──count──┬──year─┬──month──┬─week ordinal┐
│    5    │  2021 │    03   │       1     │
│    0    │  2021 │    03   │       2     │
│    0    │  2021 │    03   │       3     │
│    0    │  2021 │    03   │       4     │
└─────────┴───────┴─────────┴─────────────┘

我浏览了文档,发现了一些有趣的功能,但未能使它们解决我的问题。
我以前从未使用过 clickhouse,对 SQL 也不是很有经验,这就是我在这里寻求帮助的原因。

试试这个查询:

select count() count, toYear(start_of_month) year, toMonth(start_of_month) month,
       toWeek(start_of_week) - toWeek(start_of_month) + 1 AS "week ordinal"
from (
    select *, toStartOfMonth(login_time) start_of_month,
         toStartOfWeek(login_time) start_of_week
    from (
      /* emulate test dataset */
      select data.1 user_id, toDate(data.2) login_time
      from (
        select arrayJoin([
          (1, '2021-02-27'),            
          (1, '2021-02-28'),      
          (1, '2021-03-01'),
          (1, '2021-03-01'),
          (1, '2021-03-02'),
          (2, '2021-03-02'),
          (2, '2021-03-03'),
          (2, '2021-03-08'),
          (2, '2021-03-16'),
          (2, '2021-04-01')]) data)
      )
  )
group by start_of_month, start_of_week
order by start_of_month, start_of_week

/*
┌─count─┬─year─┬─month─┬─week ordinal─┐
│     1 │ 2021 │     2 │            4 │
│     1 │ 2021 │     2 │            5 │
│     5 │ 2021 │     3 │            1 │
│     1 │ 2021 │     3 │            2 │
│     1 │ 2021 │     3 │            3 │
│     1 │ 2021 │     4 │            1 │
└───────┴──────┴───────┴──────────────┘
*/