如何计算同一组的两个条件

时间:2016-05-31 16:12:14

标签: mysql sql

我想编写一个将显示为

的SQL查询

1)名称以“myTeam”

开头的每个组件

2)类型“Bug”

3)本月开了多少个错误,有多少错误来自该小组的后续词?

这是我的代码:

select 
histories.component_path,
count distinct bug_id as id1 from buganizer.metadata.latest WHERE (histories.component_path LIKE 'myTeam >%' And type_id = 'BUG' And (date - Now() < 30) ) as total_bugs,
count distinct bug_id as id2 from buganizer.metadata.latest WHERE (histories.component_path LIKE 'myTeam >%' And type_id = 'BUG' And status_id = 'VERIFIED') as verified_bugs

如何使这个缩短并更好地格式化?

1 个答案:

答案 0 :(得分:0)

我修改了你的查询,如下所示。 COUNT不会考虑NULL值。您可以尝试查询(未经验证)

SELECT  A.component_path, 
        COUNT(DISTINCT A.id1) AS total_bugs,
        COUNT(DISTINCT A.id2) AS verified_bugs
FROM (
    SELECT 
        histories.component_path AS component_path,
        CASE WHEN (DATE - NOW() < 30) THEN bug_id ELSE NULL END AS id1,
        CASE WHEN status_id = 'VERIFIED' THEN bug_id ELSE NULL END AS id2
    FROM buganizer.metadata.latest 
    -- need to add some join with "histories" table
    WHERE histories.component_path LIKE 'myTeam%' AND type_id = 'BUG'
) AS A
GROUP BY A.component_path