2013-01-11 60 views
0

我想從缺陷數據庫構建查詢。每個缺陷有提交日期和狀態(開,關,延期等),我想我的結果設置有3列:具有相同列的SQL查詢w/2

Date, # bugs w/ Open status, # bugs with Closed status 
2012-05-31, 3, 1 
2012-06-01, 2, 0 
2012-06-02, 3, 2 

我不知道如何超越這個查詢得到什麼我想要:

select 
    DEFECT.SUBMITTED_DATE as "Opened", 
    count (DEFECT.SUBMITTED_DATE), 
    DEFECT.STATUS as "Status" 
from 
    DEFECT 
where 
    DEFECT.STATUS='Open' or DEFECT.STATUS='Fixed' 
group by 
    DEFECT.SUBMITTED_DATE 

尋找建議。

+0

你想要三列嗎? – Woot4Moo

+0

你正在使用什麼rdbms? MySQL的? SQL Server?甲骨文? –

回答

1

在MySQL中,

SELECT DATE(DEFECT.SUBMITTED_DATE) as `DATE_SUBMITTED`, 
     SUM(CASE WHEN DEFECT.STATUS='Open' THEN 1 ELSE 0 END) `# bugs w/ Open status`, 
     SUM(CASE WHEN DEFECT.STATUS='Fixed' THEN 1 ELSE 0 END) `# bugs with Closed status`, 
FROM  DEFECT 
GROUP BY DATE(DEFECT.SUBMITTED_DATE) 

在TSQL

SELECT CAST(DEFECT.SUBMITTED_DATE as DATE) as [DATE_SUBMITTED], 
     SUM(CASE WHEN DEFECT.STATUS='Open' THEN 1 ELSE 0 END) [# bugs w/ Open status], 
     SUM(CASE WHEN DEFECT.STATUS='Fixed' THEN 1 ELSE 0 END) [# bugs with Closed status], 
FROM  DEFECT 
GROUP BY CAST(DEFECT.SUBMITTED_DATE as DATE) 
+0

hmm where子句似乎多餘 – Woot4Moo

1

嘗試:

SELECT DEFECT.SUBMITTED_DATE as DATE_SUBMITTED, 
    SUM(CASE WHEN DEFECT.STATUS = 'Open' THEN 1 ELSE 0 END) AS `# bugs w/ Open status`, 
    SUM(CASE WHEN DEFECT.STATUS = 'Fixed' THEN 1 ELSE 0 END) AS `# bugs w/ Open status` 
FROM DEFECT 
WHERE DEFECT.STATUS IN ('Open', 'Fixed') 
GROUP BY DEFECT.SUBMITTED_DATE 
+0

我給你我的[sqlfiddle](http://sqlfiddle.com/#!2/8e841/3);) – bonCodigo

+0

@bonCodigo:它與(工作)提供的查詢不匹配... :) 你什麼意思 ? (順便說一句,我加了一個逗號,我忘了) –

+0

它。 OP還沒有提供表格/樣本數據..但正確的查詢工作。 – bonCodigo

0

假設MS SQL服務器:

SELECT DEFECT.SUBMITTED_DATE as [Date] 
,SUM(CASE WHEN DEFECT.STATUS = 'Open' THEN 1 ELSE 0 END) AS number_open 
,SUM(CASE WHEN DEFECT.STATUS = 'Fixed' THEN 1 ELSE 0 END) AS number_fixed 
FROM DEFECT 
WHERE DEFECT.STATUS IN ('Open' ,'Fixed') 
GROUP BY DEFECT.SUBMITTED_DATE 
相關問題