Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL: Count the distinct rows per day

I have an interesting query I need to do. I have a table with an INT column containing ip address numbers (using INET_ATON), and a timestamp column. I want to be able to count the number of unique ip address columns there are per day. That is, how many distinct ip rows there are in each day. So, for example, if an ip address is in the same day twice, it counts as 1 in the final count, however if the same ip address is in another day it'll be counted there will be a second count for it.

Example Data:

PK | FK  | ipNum      | timestamp
11 | 404 | 219395     | 2013-01-06 22:23:56
7  | 404 | 467719     | 2013-01-06 22:23:41
8  | 404 | 4718869    | 2013-01-06 22:23:42
10 | 404 | 16777224   | 2013-01-06 22:23:56
5  | 404 | 1292435475 | 2013-01-06 22:23:25
12 | 404 | 1526990605 | 2013-01-06 22:23:57
6  | 404 | 1594313225 | 2013-01-06 22:23:40
4  | 404 | 1610613001 | 2013-01-06 22:23:23
9  | 404 | 1628635192 | 2013-01-06 22:23:55
1  | 404 | 2130706433 | 2013-01-06 21:29:38
2  | 407 | 2130706433 | 2013-01-06 21:31:59
3  | 407 | 2130706433 | 2013-01-06 21:32:22
like image 942
Sam Avatar asked Jan 07 '13 06:01

Sam


People also ask

How do I count distinct rows in MySQL?

MySQL COUNT(DISTINCT) function returns a count of number rows with different non-NULL expr values. Where expr is a given expression. The following MySQL statement will count the unique 'pub_lang' and average of 'no_page' up to 2 decimal places for each group of 'cate_id'.

How do I count distinct rows in SQL?

We can use SQL Count Function to return the number of rows in the specified condition. The syntax of the SQL COUNT function: COUNT ([ALL | DISTINCT] expression); By default, SQL Server Count Function uses All keyword.

Can we use count with distinct?

Yes, you can use COUNT() and DISTINCT together to display the count of only distinct rows.

Why is Count 1 faster than count (*)?

The simple answer is no – there is no difference at all. The COUNT(*) function counts the total rows in the table, including the NULL values. The semantics for COUNT(1) differ slightly; we'll discuss them later. However, the results for COUNT(*) and COUNT(1) are identical.


2 Answers

SELECT  DATE(timestamp) Date, COUNT(DISTINCT ipNum) totalCOunt FROM    tableName GROUP   BY  DATE(timestamp) 
  • SQLFiddle Demo
like image 88
John Woo Avatar answered Sep 29 '22 17:09

John Woo


Here's how you'd get counts per day for the last 7 days:

select
    count(*) as count,
    date(timestamp) as date
from
    tablename
where
    timestamp >= date_sub(curdate(), interval 7 day)
group by 
    date;

+-------------+------------+
| count       | date       |
+-------------+------------+
| #forThatDay | 2020-02-21 |
| #forThatDay | 2020-02-22 |
| #forThatDay | 2020-02-22 |
| #forThatDay | 2020-02-23 |
| #forThatDay | 2020-02-24 |
| #forThatDay | 2020-02-25 |
| #forThatDay | 2020-02-26 |
+-------------+------------+
7 rows in set (0.03 sec)

group by the ipNum column first to get distinct counts of that column per day:

select
    count(*) as count,
    date(timestamp) as date
from
    tablename
where
    timestamp >= date_sub(curdate(), interval 7 day)
group by 
    ipNum, date;

like image 25
giantNinja Avatar answered Sep 29 '22 18:09

giantNinja