DROP TABLE IF EXISTS test;
CREATE TABLE test( id INT, entrydate DATE );
INSERT INTO test VALUES
( 1, '2007-5-01' ),( 1, '2007-5-02' ),
( 1, '2007-5-03' ),( 1, '2007-5-04' ),
( 1, '2007-5-05' ),( 1, '2007-5-06' ),
( 2, '2007-6-01' ),( 2, '2007-6-02' ),
( 2, '2007-6-03' ),( 2, '2007-6-04' ),
( 3, '2007-7-01' ),( 3, '2007-7-02' ),
( 3, '2007-7-03' );
-- expected result
+------+------------+------+
| id | entrydate | rank |
+------+------------+------+
| 1 | 2007-05-01 | 1 |
| 1 | 2007-05-02 | 2 |
| 2 | 2007-06-01 | 1 |
| 2 | 2007-06-02 | 2 |
| 3 | 2007-07-01 | 1 |
| 3 | 2007-07-02 | 2 |
+------+------------+------+
drop table if exists t;
create table t (
type int(10) ,
instance int(10)
) ;
insert into t values
(1,4),(1,7),(1,9),(1,10),(2,2),
(2,3),(2,5),(2,6),(2,8),(3,1),(4,11);
select
group_concat(concat(type,'(',qty,')') separator ', ')
as list
from (
select type, count(*) qty
from t
group by type
) n
+------------------------+
| list |
+------------------------+
| 1(4), 2(5), 3(1), 4(1) |
+------------------------+