i have a list with peoples id and date, the list say when a person Entered to website (his id and date). how can i show for all the dates how many people enter the site two days in a row?
the data ( 30,000 like this in diffrent dates) 01/03/2019 4616 01/03/2019 17584 01/03/2019 7812 01/03/2019 34 01/03/2019 12177 01/03/2019 7129 01/03/2019 11660 01/03/2019 2428 01/03/2019 17514 01/03/2019 10781 01/03/2019 7629 01/03/2019 11119
I succeeded to show the amount of pepole enter the site on the same day but i didnt succeeded to add a column that show the pepole that enter 2 days in row.
date number_of_entrance 2019-03-01 7099 2019-03-02 7021 2019-03-03 7195 2019-03-04 7151 2019-03-05 7260 2019-03-06 7169 2019-03-07 7076 2019-03-08 7081 2019-03-09 6987 2019-03-10 7172 select date,count(*) as number_of_entrance fROM [finalaa].[dbo].[Daily_Activity] group by Date order by date;
Advertisement
Answer
how can i show for all the dates how many people enter the site two days in a row?
I would just use lag()
:
select count(distinct person) from (select t.*, lag(date) over (partition by person order by date) as prev_date from t ) t where prev_date = dateadd(day, -1, date);
Your code suggests SQL Server, so I used the date functions in that database.
If you want this per date:
select date, count(distinct person) from (select t.*, lag(date) over (partition by person order by date) as prev_date from t ) t where prev_date = dateadd(day, -1, date) group by date;