MySql: the number of rows that have the same data in a column

I am trying to select everything in a table, and also count the number of rows in a table with the same data.

SELECT *, COUNT(thedate) daycount FROM `table` ORDER BY thedate DESC

My hope is to have one query that prints the date and number of rows associated with that date, and the loop result would be something like this:

January 1, 2000 (2 lines)
col1, col2, col3, col4
col1, col2, col3, col4

January 1, 2000 (3 lines)
col1, col2, col3, col4
col1, col2, col3, col4
col1, col2, col3, col4

January 1, 2000 (6 lines)
col1, col2, col3, col4
col1, col2, col3, col4
col1, col2, col3, col4
col1, col2, col3, col4
col1, col2, col3, col4
col1, col2, col3, col4

etc...

It makes sense?
+5
source share
5 answers

If you have a table that looks like this:

CREATE TABLE yourtable
(
    datefield DATETIME,
    col1 VARCHAR(20),
    col2 INT NOT NULL,
    col3 TINYINT NOT NULL,
    col4 CHAR(5)
);

and you wanted to count the number of duplicates col1 .. col4 for every day, you should run this query

SELECT
    COUNT(datefield) datefield_count,
    LEFT(all_fields,10) datefield,
    SUBSTR(all_fields,11) all_other_fields
FROM
(
    SELECT
        DATE(datefield) datefield,
        CONCAT(DATE(datefield),'|',
        COALESCE(col1,'< NULL >'),'|',
        COALESCE(col2,'< NULL >'),'|',
        COALESCE(col3,'< NULL >'),'|',
        COALESCE(col4,'< NULL >'),'|') all_fields
    FROM
         yourtable
) A
GROUP BY all_fields;

Here are some sample data and query result:

mysql> DROP TABLE IF EXISTS yourtable;
Query OK, 0 rows affected (0.04 sec)

mysql> CREATE TABLE yourtable
    -> (
    ->     datefield DATETIME,
    ->     col1 VARCHAR(20),
    ->     col2 INT,
    ->     col3 TINYINT,
    ->     col4 CHAR(5)
    -> );
Query OK, 0 rows affected (0.11 sec)

mysql> INSERT INTO yourtable VALUES
    -> (DATE(NOW() - INTERVAL 1 DAY),'rolando',4,3   ,'angel'),
    -> (DATE(NOW() - INTERVAL 1 DAY),'rolando',4,3   ,'angel'),
    -> (DATE(NOW() - INTERVAL 1 DAY),'rolando',4,3   ,'angel'),
    -> (DATE(NOW() - INTERVAL 1 DAY),'rolando',4,NULL,'angel'),
    -> (DATE(NOW() - INTERVAL 1 DAY),'rolando',4,NULL,'angel'),
    -> (DATE(NOW() - INTERVAL 2 DAY),'rolando',4,2   ,'angel'),
    -> (DATE(NOW() - INTERVAL 2 DAY),'rolando',4,2   ,'angel'),
    -> (DATE(NOW() - INTERVAL 2 DAY),'rolando',4,2   ,'angel'),
    -> (DATE(NOW() - INTERVAL 2 DAY),'rolando',4,2   ,'angel'),
    -> (DATE(NOW() - INTERVAL 2 DAY),'rolando',4,NULL,'edwards'),
    -> (DATE(NOW() - INTERVAL 2 DAY),'rolando',4,NULL,'angel'),
    -> (DATE(NOW() - INTERVAL 3 DAY),'rolando',5,2   ,'angel'),
    -> (DATE(NOW() - INTERVAL 3 DAY),'rolando',5,2   ,'angel'),
    -> (DATE(NOW() - INTERVAL 3 DAY),'rolando',4,2   ,'angel'),
    -> (DATE(NOW() - INTERVAL 3 DAY),'pamela' ,4,2   ,'angel'),
    -> (DATE(NOW() - INTERVAL 3 DAY),'pamela' ,4,NULL,'edwards'),
    -> (DATE(NOW() - INTERVAL 3 DAY),'pamela' ,5,2   ,'angel'),
    -> (DATE(NOW() - INTERVAL 3 DAY),'pamela' ,5,2   ,'angel'),
    -> (DATE(NOW() - INTERVAL 3 DAY),'rolando',4,2   ,'angel'),
    -> (DATE(NOW() - INTERVAL 3 DAY),'rolando',4,2   ,'angel'),
    -> (DATE(NOW() - INTERVAL 3 DAY),'rolando',4,NULL,'edwards'),
    -> (DATE(NOW() - INTERVAL 3 DAY),'rolando',4,NULL,'angel')
    -> ;
Query OK, 22 rows affected, 3 warnings (0.03 sec)
Records: 22  Duplicates: 0  Warnings: 3

mysql> SELECT * FROM yourtable;
+---------------------+---------+------+------+-------+
| datefield           | col1    | col2 | col3 | col4  |
+---------------------+---------+------+------+-------+
| 2011-06-30 00:00:00 | rolando |    4 |    3 | angel |
| 2011-06-30 00:00:00 | rolando |    4 |    3 | angel |
| 2011-06-30 00:00:00 | rolando |    4 |    3 | angel |
| 2011-06-30 00:00:00 | rolando |    4 | NULL | angel |
| 2011-06-30 00:00:00 | rolando |    4 | NULL | angel |
| 2011-06-29 00:00:00 | rolando |    4 |    2 | angel |
| 2011-06-29 00:00:00 | rolando |    4 |    2 | angel |
| 2011-06-29 00:00:00 | rolando |    4 |    2 | angel |
| 2011-06-29 00:00:00 | rolando |    4 |    2 | angel |
| 2011-06-29 00:00:00 | rolando |    4 | NULL | edwar |
| 2011-06-29 00:00:00 | rolando |    4 | NULL | angel |
| 2011-06-28 00:00:00 | rolando |    5 |    2 | angel |
| 2011-06-28 00:00:00 | rolando |    5 |    2 | angel |
| 2011-06-28 00:00:00 | rolando |    4 |    2 | angel |
| 2011-06-28 00:00:00 | pamela  |    4 |    2 | angel |
| 2011-06-28 00:00:00 | pamela  |    4 | NULL | edwar |
| 2011-06-28 00:00:00 | pamela  |    5 |    2 | angel |
| 2011-06-28 00:00:00 | pamela  |    5 |    2 | angel |
| 2011-06-28 00:00:00 | rolando |    4 |    2 | angel |
| 2011-06-28 00:00:00 | rolando |    4 |    2 | angel |
| 2011-06-28 00:00:00 | rolando |    4 | NULL | edwar |
| 2011-06-28 00:00:00 | rolando |    4 | NULL | angel |
+---------------------+---------+------+------+-------+
22 rows in set (0.00 sec)

mysql> SELECT
    ->     COUNT(datefield) datefield_count,
    ->     LEFT(all_fields,10) datefield,
    ->     SUBSTR(all_fields,11) all_other_fields
    -> FROM
    -> (
    ->     SELECT
    ->         DATE(datefield) datefield,
    ->         CONCAT(DATE(datefield),'|',
    ->         COALESCE(col1,'< NULL >'),'|',
    ->         COALESCE(col2,'< NULL >'),'|',
    ->         COALESCE(col3,'< NULL >'),'|',
    ->         COALESCE(col4,'< NULL >'),'|') all_fields
    ->     FROM
    ->          yourtable
    -> ) A
    -> GROUP BY all_fields;
+-----------------+------------+----------------------------+
| datefield_count | datefield  | all_other_fields           |
+-----------------+------------+----------------------------+
|               1 | 2011-06-28 | |pamela|4|2|angel|         |
|               1 | 2011-06-28 | |pamela|4|< NULL >|edwar|  |
|               2 | 2011-06-28 | |pamela|5|2|angel|         |
|               3 | 2011-06-28 | |rolando|4|2|angel|        |
|               1 | 2011-06-28 | |rolando|4|< NULL >|angel| |
|               1 | 2011-06-28 | |rolando|4|< NULL >|edwar| |
|               2 | 2011-06-28 | |rolando|5|2|angel|        |
|               4 | 2011-06-29 | |rolando|4|2|angel|        |
|               1 | 2011-06-29 | |rolando|4|< NULL >|angel| |
|               1 | 2011-06-29 | |rolando|4|< NULL >|edwar| |
|               3 | 2011-06-30 | |rolando|4|3|angel|        |
|               2 | 2011-06-30 | |rolando|4|< NULL >|angel| |
+-----------------+------------+----------------------------+
12 rows in set (0.00 sec)

mysql>

I will leave this creative work for you to go through and print.

  • Datefield
  • datefield_count
  • print all_other_fields 'datefield_count' times

Give it a try !!!

+3
source

This is not what the OP asked about, but exactly what I was looking for when I came across this question. Some may find this useful.

select *
  from thetable
    join (
      select thedate, count( thedate ) as cnt
        from thetable
        group by thedate
    ) as counts
    using( thedate )
  order by thedate

cnt, , . - :

, 2
col1, col2, col3, col4
col1, col2, col3, col4

, 3
col1, col2, col3, col4
col1, col2, col3, col4
col1, col2, col3, col4

- , 1
col1, col2, col3, col4

+1
SELECT ...
FROM yourtable
GROUP BY DATE(datefield)
ORDER BY COUNT(DATE(datefield)) DESC

, DATE(), , - . , yyyy-mm-dd hh: mm: ss, yyyy-mm-dd, .

This will give you the main results. Performing the output as you wish will require some further processing in your script, but not too complicated. Just buffer the found lines until the date changes, and then output a buffer with line counting.

0
source
SELECT *, COUNT(thedate) daycount 
FROM `table` 
GROUP BY thedate
ORDER BY thedate DESC
0
source
SELECT thedate, COUNT(id)
FROM table
WHERE 1
GROUP BY thedate
ORDER BY thedate
0
source

All Articles