2016-05-31 39 views
0

我想寫一個SQL查詢同一組數兩個條件,將展現給如何在

1)名稱以類「myTeam」

2)「錯誤」

每個組件

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 

我怎樣才能使這個短多格式正確?

+2

你真的應該向我們展示的表結構與一些樣本數據一起。 –

+0

如果這個工程,那麼它不是MySQL – Strawberry

+0

@Strawberry我不認爲這個代碼工作..不是SQL ..太 – scaisEdge

回答

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