简体   繁体   中英

Count the same id with group by in one single column

I have table kondisi like this

+------------+----------------+
| id_kondisi | id_sub_kondisi |
+------------+----------------+
| 01         | 0102           |
| 03         | 0302           |
| 01         | 0101           |
| 01         | 0102           |
| 01         | 0101           |
| 03         | 0301           |
| 03         | 0303           |
| 02         | 0202           |
| 01         | 0102           |
| 03         | 0301           |
| 01         | 0101           |
| 02         | 0203           |
| 03         | 0302           |
| 02         | 0202           |
| 02         | 0201           |
| 02         | 0202           |
+------------+----------------+
16 rows in set (0.00 sec)

I want to a result of the table that looks like this

+----------------+-------------+
| kondisi_tot    | coun_tot    |
+----------------+-------------+
| 01             |  6          |
| 0101           |  3          |
| 0102           |  3          |
| 02             |  5          |
| 0201           |  1          |
| 0202           |  3          |
| 0203           |  1          |
| 03             |  5          |
| 0301           |  2          |
| 0302           |  2          |
| 0303           |  1          |
+----------------+-------------+

SO i need to count the data id that have been looping. Just like the above result i know i have to use group by but how do i make the other column into one column ?

PS : My id_kondisi and id_sub_kondisi is a char type, not int type

SELECT ID, COUNT(*) FROM (
   SELECT id_kondisi as ID FROM kondisi
   UNION ALL
   SELECT id_sub_kondisi as ID FROM kondisi
) sub
GROUP BY ID
ORDER BY ID

Can have something to do with ordering cause having an ID as a number 02 will be less than 0102. Maybe you need to convert an ID to string in "order by" statement.

Something like this should work. Note that UNION ALL is used to make sure all values are counted; the ID value is then casted to CHAR to make the sort order work.

SQL Fiddle

MySQL 5.6 Schema Setup :

CREATE TABLE kondisi
    (`id_kondisi` int, `id_sub_kondisi` int)
;

INSERT INTO kondisi
    (`id_kondisi`, `id_sub_kondisi`)
VALUES
    (01, 0102),
    (03, 0302),
    (01, 0101),
    (01, 0102),
    (01, 0101),
    (03, 0301),
    (03, 0303),
    (02, 0202),
    (01, 0102),
    (03, 0301),
    (01, 0101),
    (02, 0203),
    (03, 0302),
    (02, 0202),
    (02, 0201),
    (02, 0202)
;

Query 1 :

select id, count(id) 
from 
(select id_kondisi as id from kondisi
union all
select id_sub_kondisi from kondisi) merged_table
group by id
order by cast(id as char) 

Results :

    |  id | count(id) |
    |-----|-----------|
    |   1 |         6 |
    | 101 |         3 |
    | 102 |         3 |
    |   2 |         5 |
    | 201 |         1 |
    | 202 |         3 |
    | 203 |         1 |
    |   3 |         5 |
    | 301 |         2 |
    | 302 |         2 |
    | 303 |         1 |

You need to combine the result set of these two column using UNION :

Following UNION query retuns result of two columns:

SELECT usr_user_type_removed as id from user UNION ALL select usr_status from user

Full Query:

SELECT id, COUNT(id) FROM 
(SELECT usr_user_type_removed as id from user UNION ALL select usr_status from user ) n_table
group by 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM