在一个列中计算相同的id,而其他列具有where子句

时间:2017-09-14 16:27:11

标签: mysql multiple-columns

我有question就像这样,已经得到了解答。

现在,我有一个新列但是同一个表(现在有一个日期)

+------------+----------------+-------------+
| id_kondisi | id_sub_kondisi | tgl_kondisi |
+------------+----------------+-------------+
| 01         | 0102           | 2017-09-13  |
| 03         | 0302           | 2017-09-13  |
| 01         | 0101           | 2017-09-13  |
| 01         | 0102           | 2017-09-13  |
| 01         | 0101           | 2017-09-13  |
| 03         | 0301           | 2017-09-13  |
| 03         | 0303           | 2017-09-13  |
| 02         | 0202           | 2017-09-14  |
| 01         | 0102           | 2017-09-13  |
| 03         | 0301           | 2017-09-13  |
| 01         | 0101           | 2017-09-13  |
| 02         | 0203           | 2017-09-14  |
| 03         | 0302           | 2017-09-13  |
| 02         | 0202           | 2017-09-14  |
| 02         | 0201           | 2017-09-14  |
| 02         | 0202           | 2017-09-14  |
+------------+----------------+-------------+

查询

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

INSERT INTO kondisi
    (`id_kondisi`, `id_sub_kondisi`, `tgl_kondisi`)
VALUES
    (01, 0102, 2017-09-13),
    (03, 0302, 2017-09-13),
    (01, 0101, 2017-09-13),
    (01, 0102, 2017-09-13),
    (01, 0101, 2017-09-13),
    (03, 0301, 2017-09-13),
    (03, 0303, 2017-09-13),
    (02, 0202, 2017-09-14),
    (01, 0102, 2017-09-13),
    (03, 0301, 2017-09-13),
    (01, 0101, 2017-09-13),
    (02, 0203, 2017-09-14),
    (03, 0302, 2017-09-13),
    (02, 0202,  2017-09-14),
    (02, 0201,  2017-09-14),
    (02, 0202, 2017-09-14)
;

如果count_tottgl_count1,我如何在tgl_kondisi中汇总已循环的ID的数量,并在2017-09-13列中包含where子句。我想要的结果是这样的:

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

我做了类似的事,但它没有用

select id, count(id), (select count(id) from kondisi where tgl_kondisi='2017-09-13') 
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) 

1 个答案:

答案 0 :(得分:0)

所以正确的查询将是

SELECT 
     id, 
     COUNT(id), 
     SUM(
          CASE WHEN 
               tgl_kondisi='2017-09-13' THEN 1 ELSE 0 END
FROM 
(   SELECT 
         id_kondisi as id,
         tgl_kondisi
    FROM kondisi
           UNION ALL

    SELECT 
         id_sub_kondisi,
         tgl_kondisi
    FROM kondisi
) 
merged_table
GROUP BY id
ORDER BY cast(id as char) 

我改变了我的

SELECT COUNT(id) FROM kondisi WHERE tgl_kondisi='2017-09-13')

进入

SELECT id, COUNT(id), SUM(CASE WHEN tgl_kondisi='2017-09-13' THEN 1 ELSE 0 END

并在tgl_kondisi

中添加UNION

感谢PM77-1