I have an interesting query here. I have a table that stores visitor's ip
and page_id
along with a timestamp ( date
). I would like to count the amount of visitors per day for each page_id
so I can then take that output and calculate which page_id
is trending (on a daily basis)
Table visitors_counter
looks like this:
id|page_id | ip | date
1 | 37 |1.1.1.1| 2017-02-10 14:03:16
2 | 38 |1.2.1.1| 2017-02-10 11:04:16
3 | 39 |1.1.3.1| 2017-02-10 16:05:16
4 | 37 |1.5.1.1| 2017-02-10 17:08:16
5 | 37 |1.1.1.1| 2017-02-10 19:07:16
And what I would like to achieve would be something like:
id|page_id |visitors | date
1 | 37 |3 | 2017-02-10 14:03:16
2 | 38 |1 | 2017-02-10 11:04:16
3 | 39 |1 | 2017-02-10 16:05:16
So far I've been able to count the amount of unique visitors per day with
SELECT DATE(date) Date, COUNT(DISTINCT page_id) uniqueperday FROM visitors_counter GROUP BY DATE(date)
i know im close, but its not quite what I want as I don't know which page_id
are the most visited ones
Thanks
Simply add page_id
in the GROUP BY
clause:
SELECT DATE(date) Date, page_id, COUNT(DISTINCT ip) uniqueperday
FROM visitors_counter
GROUP BY DATE(date), page_id
The above query returns the number of unique ip
visits per page per day.
for your result you should use the group by and count(*) (you have two times the same id for page 37 and want result 3)
SELECT DATE(date) Date, page_id, COUNT(*) visitperday
FROM visitors_counter
GROUP BY DATE(date), page_id
othewise the Giorgios is the right one
You can use aggregation on page and date to find count of distinct visitors per page per day. Also, use user variables to generate sequence id.
set @id := 0;
select @id = @id + 1 id,
page_id,
count(distinct ip) visitors,
min(date)
from visitors_counter
group by page_id, DATE(date)
order by visitors desc, page_id;
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.