错误代码:1235此版本的mariadb尚不支持“count(distinct)aggregate as window function”

relj7zay  于 2021-06-23  发布在  Mysql
关注(0)|答案(1)|浏览(489)

我在mariadb上有下表:

date              name

2018-01-02        John
2018-01-03        Mary
2018-01-05        Peter
2018-01-06        John
2018-01-07        Ann
2018-01-08        Ann
2018-01-09        John
2018-01-10        Peter

我想计算每天的累计人数。i、 e.我希望我的结果是:

date        cumulated_name_count

2018-01-02        1
2018-01-03        2
2018-01-04        2
2018-01-05        3
2018-01-06        3
2018-01-07        4
2018-01-08        4
2018-01-09        4
2018-01-10        4

为了实现这一点,我使用以下带有窗口函数的代码:

select date, 
  count(distinct name) over (order by time rows unbounded preceding) 
  as cumulated_name_count from myDB.myTable

但是,我得到了以下错误:

Error Code: 1235. This version of MariaDB doesn't yet support 'COUNT(DISTINCT) aggregate as window function'

有没有一种方法可以避免这种错误并获得窗口函数的累积计数?谢谢!

svmlkihl

svmlkihl1#

可以使用嵌套窗口函数执行此操作:

select date, 
       sum( seqnum = 1 ) over (order by time rows unbounded preceding)
              as cumulated_name_count
    from (select  t.*,
                  row_number() over (partition by name order by time)
                        as seqnum
              from myDB.myTable t
         ) t2;

子查询枚举每个 name 时间价值。外部查询然后统计每个记录的“1”个数。

相关问题