簡體   English   中英

在MySQL數據庫中count(distinct(person_id))無法與窗口函數一起使用

[英]count(distinct(person_id)) not working with window function in MySQL database

因此,我發現mysql不支持將聚集函數(如count(distinct(person_id)))與窗口函數一起使用。 例如,以下查詢將不起作用。

select count(distinct(person_id)) over ([OrderBy clause])
from <table>;

像窗口函數一樣快速工作的替代方法是什么?

模式:

create table table1(
check_date date,
person_id  varchar(10)
);

我嘗試查詢:

select person_id,count(distinct(person_id))
over (order by check_date range between interval '20' day preceding and current row)
from table1;

需要在20天的時間范圍內獲得所有簽入系統的不同人員的計數。

這回答了問題的原始版本。

在MySQL 8+中,您可以使用以下兩個窗口函數進行仿真:

select sum(seqnum = 1) over (order by ?) as num_distinct
from (select t.*,
             row_number() over (partition by person_id order by ?) as seqnum
      from <table> t
     ) t;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM