简体   繁体   中英

SQL window-over by-incremental distinct users

I am sure this must be fairly easy for you but unfortunately it is not for me !

I am trying to write a query that counts incremental distinct user id grouped by month.

Understand if user X has a row in both january and february he should be counted as 1 in January but not in February.

I can do the following below for a given month but I would like to automate it

EDIT:

Let me try to clarify: a row in table UX is created every time a user performs a given action. I would like to count the number of unique NEW(/incremental) users every month who performed this action. Meaning if user A performed this action in January AND February he would only be counted in January.

select 
  count(distinct ux.account_id)
, trunc(ux.date_key,'MM') as month
from 
ux

left join 

(
  select 
  distinct ux.account_id as account_id
from 
 ux

where 

and ux.date_key < '2019-02-01'

) bf on ux.account_id=bf.account_id

where 
    and ux.date_key >= '2019-02-01'
    and bf.account_id IS NULL

group by 
  trunc(ux.date_key,'MM')

"incremental distinct user" to me sounds a lot like a user starting. Does this do what you want?

select trunc(min_date_key, 'MM') as month
from (select account_id, min(ux.date_key) as min_date_key
      from ux
      group by account_id
     ) ux
where min_date_key < '2019-02-01' and ux.account_id IS NULL
group by trunc(ux.date_key, 'MM')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM