简体   繁体   中英

Counting Distinct users over time

Data is thus:

Date       | userid 
-------------------
01/01/2016 | jacby
01/01/2016 | jacby
01/01/2016 | donta
01/01/2016 | stapf
02/02/2016 | kamed
02/02/2016 | jacby
02/02/2016 | kamed
02/02/2016 | carpi
03/03/2016 | slwig
03/03/2016 | kamed

What I would like to be able to do is produce an output, using SQL, TSQL or SSRS expressions, that looks like:

Date       | Unique Users
-----------------------------
01/01/2016 | 3
02/02/2016 | 5
03/03/2016 | 6

which is essentially a running total of unique users, in reference to unique users up to that date. Ie from a start day, i want to know the total number of unique users since the start date, ongoing.

I can do a running total of unique users on a particular day, but that doesn't account for if that user had logged in on a previous day. I did wonder about a while loop using the date as a counter, but really, I don't know where to start with that.

My google-fu is way off and I can't think of the correct terminology to describe what I am looking for.

EDIT - Apologies - there may be more than one entry per day for each user. I have amended the source example table above.

You want to get the first time someone signed up and then take a cumulative sum of this value:

select t.date,
       sum(sum(case when seqnum = 1 then 1 else 0 end)) over (order by date) as numUniques
from (select t.*, row_number() over (partition by user order by date) as seqnum
      from t
     ) t
group by t.date
order by t.date;

You can put a where clause in the subquery to restrict this to a particular set of dates.

Here is one way to do this.

if OBJECT_ID('tempdb..#Something') is not null
    drop table #Something

create table #Something
(
    MyDate date
    , UserID varchar(10)
)

insert #Something (MyDate, UserID) values
('01/01/2016', 'jacby'),
('01/01/2016', 'donta'),
('01/01/2016', 'stapf'),
('02/02/2016', 'kamed'),
('02/02/2016', 'jacby'),
('02/02/2016', 'carpi'),
('03/03/2016', 'slwig'),
('03/03/2016', 'kamed')

select distinct s.MyDate
    , 
    (
        select count(distinct UserID)
        from #Something s2
        where s2.MyDate <= s.MyDate
    ) as UniqueUsers
from #Something s

Below query will give the required output :-

declare @table_name table
(Date date NOT NULL,
userid varchar(20) NOT NULL)

Insert into @table_name
values('01/01/2016','jacby'),
('01/01/2016','donta'),
('01/01/2016','stapf'),
('02/02/2016','kamed'),
('02/02/2016','jacby'),
('02/02/2016','carpi'),
('03/03/2016','slwig'),
('03/03/2016','kamed')



select distinct Date,
(select count(distinct userid) from @table_name TM where TM.Date<=TN.Date) [Unique Users]
from @table_name TN

Output :

  Date      Unique Users
2016-01-01      3
2016-02-02      5
2016-03-03      6
select d2.date, count(distinct d2.userid) 
  from data d1 
  join data d2 
        on d2.date <= d1.date 
 group by d2.date

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