4

我有一个包含每个条目的表incident_idstatus(假设是opencloseddate_raised(日期)和closure_date(日期)。

我想显示一个表格,计算在关闭日期关闭的事件数量(因此计数incident_idwhere status='closed' and closure_date is not null),以及保持打开的事件数量(在同一天的incident_idwhere计数。status='open'

如果我把你弄糊涂了,一个看起来像这样的表:

 ______________________________________________________________________________
| closure date | count of incidents closed | count of incidents remaining open |
|--------------|---------------------------|-----------------------------------|
| 01-Sep-12    | 5                         | 14                                |
| ...          | ...                       | ...                               |

我已经管理了一个表,它可以像这样计算关闭的事件数:

SELECT COUNT(incident_id)
WHERE closure_date IS NOT NULL AND status="open"
GROUP BY closure_date

我已经尝试了几个小时来让其他计数工作,但到目前为止还不能:-(

编辑:这是我拥有的表的示例:

 ___________________________________________________
| incident_id | status | date_raised | closure_date |
|-------------|--------|-------------|--------------|
| 1           | closed | 01-Sep-12   | 01-Sep-12    |
| 2           | open   | 30-Aug-12   | (null)       |
| 3           | open   | 02-Sep-12   | (null)       |
| 4           | closed | 02-Sep-12   | 05-Sep-12    |
| ...         | ...    | ...         | ...          |

会给桌子:

 ______________________________________________________________________________
| closure date | count of incidents closed | count of incidents remaining open |
|--------------|---------------------------|-----------------------------------|
| 01-Sep-12    | 1                         | 1                                 |
| 05-Sep-12    | 1                         | 2                                 |
4

3 回答 3

1
SELECT 
COUNT(*) as closed,
(SELECT COUNT(t2.incident_id) FROM TABLE t2 WHERE t2.status = 'open' and t2.raised_date < t1.closure_date) as open
FROM TABLE t1
WHERE 
t1.status = 'closed'
GROUP BY t1.closure_date

或相同的想法

WITH opened AS (SELECT COUNT(t2.incident_id) as cnt FROM table t2 WHERE STATUS = 'open' )
SELECT 
   to_char(closure_date, 'yyyy/mm/dd') as dte
   count(*) as closed,
   opened.cnt as opens
FROM table, opened
WHERE status = 'closed'
GROUP BY to_char(closure_date, 'yyyy/mm/dd'), opened.cnt
于 2012-09-13T13:33:14.893 回答
1

在我看来,对于每个日期,您都希望获得迄今为止已关闭的问题数量以及在该日期之前提出的仍未解决的问题数量,对吗?所以你可能想要这样的东西:

SELECT t1.closure_date, COUNT(t1.incident_id)
     , ( SELECT COUNT(t2.incident_id) FROM incident_table t2
          WHERE t2.status = 'open'
            AND t2.raised_date < t1.closure_date )
  FROM incident_table t1
 WHERE t1.closure_date IS NOT NULL
   AND t1.status = 'closed'
 GROUP BY t1.closure_date
于 2012-09-13T14:23:58.717 回答
0

你应该尝试这样的事情:

SELECT 
closure_date,
SUM(CASE WHEN status="open" 
         THEN 1 ELSE 0 END) AS "count of incidents open",
SUM(CASE WHEN  status="closed" 
         THEN 1 ELSE 0 END) AS "count of incidents closed"
FROM YourTable
WHERE closure_date IS NOT NULL
GROUP BY closure_date
于 2012-09-13T13:32:30.903 回答