Oracle SQL帮助计算两种不同的方式,但具有共同的分组

我有一个表,其中包含每个条目的incident_id,status(比如打开或关闭,date_raised(Date)和closure_date(Date).

我想显示一个表来计算关闭日期关闭的事件数(因此,其中status =’closed’且closure_date不为null的incident_id的计数),以及保持打开的事件数(events_id的状态,其中状态=同一天’打开’.

万一我困惑了你,一个看起来像这样的表:

 ______________________________________________________________________________
| 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                                 |

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

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
点赞