0

我想从缺陷数据库构建查询。每个缺陷都有一个提交日期和一个状态(打开、关闭、延迟等)。我希望我的结果集有 3 列:

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

寻找建议。

4

3 回答 3

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)
于 2013-01-11T14:52:38.747 回答
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
于 2013-01-11T14:52:47.717 回答
0

假设 MS SQL Server:

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
于 2013-01-11T14:54:40.880 回答