
我有一個訪問過我的平臺的 user_id 表。我只想為每個用戶計算那些訪問我的商店 4 次或更多次的用戶 ID,并且每天持續 10 天。為了實作這一點,我使用了這個查詢:
select date(arrival_timestamp), count(user_id)
from mytable
where date(arrival_timestamp) >= current_date-10
and date(arrival_timestamp) < current_date
group by 1
having count(user_id)>=4
order by 2 desc
limit 10;
但是這個查詢實際上是獲取計數值大于 4 的所有用戶,而不是每天覆寫幾乎每個用戶,因此我不能只隔離那些在特定日期訪問我的商店不止一次的用戶。在這方面的任何幫助表示贊賞。
謝謝
uj5u.com熱心網友回復:
你可以試試這個
with list as (
select user_id, count(*) as user_count, array_agg(arrival_timestamp) as arrival_timestamp
from mytable
where date(arrival_timestamp) >= current_date-10
and date(arrival_timestamp) < current_date
group by user_id)
select user_id, unnest(arrival_timestamp)
from list
where user_count >= 4
uj5u.com熱心網友回復:
從過去 10 天內(內部查詢)每天訪問您的商店 4 次或更多次的日常用戶串列中,選擇出現 10 次(即每天)的用戶。
select user_id
from
(
select user_id
from the_table
where arrival_timestamp::date between current_date - 10 and current_date - 1
group by user_id, arrival_timestamp::date
having count(*) >= 4
) t
group by user_id
having count(*) = 10;
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/337129.html
標籤:sql PostgreSQL 通过...分组 数数 聚合函数
