4

我需要在 Oracle APEX 中生成类似于以下示例的报告:

 PROJECT     OPEN_ISSUE_COUNT     CLOSED_ISSUE_COUNT
 W-1         3                    1
 X-2         1                    2
 Y-3         5                    3
 Z-4         2                    1

whereOPEN_ISSUE_COUNTCLOSED_ISSUE_COUNT是由SQL COUNT语句生成的。被查询的表如下所示:

 ISSUE_#     ISSUE_STATUS     ASSOCIATED_PROJECT
 1A          OPEN             W-1
 1B          OPEN             W-1
 1C          OPEN             W-1
 2A          CLOSED           W-1
 2B          OPEN             X-2
 2C          CLOSED           X-2
 3A          CLOSED           X-2 
 etc...

所以在一个查询中,我需要分别计算OPEN_ISSUE_COUNTCLOSED_ISSUE_COUNTwhereISSUS_STATUS = 'OPEN'ISSUS_STATUS = 'CLOSED'GROUP BY ASSOCIATED_PROJECT

那有意义吗?显然,对于这两种状态之一,我可以轻松地做到这一点,但是对于我在这里描述的内容,我无法提出任何可行的解决方案。我在这里和其他地方在线查看了一些东西,但没有看到类似的东西。让我知道你们的想法。谢谢!

4

3 回答 3

8

由于count()只计算非空值,这应该有效:

select associated_project as project,
count(case when issue_status='OPEN' then 1 else null end) as open_issue_count,
count(case when issue_status='CLOSED' then 1 else null end) as closed_issue_count
from table
group by associated_project;

当然,这假定 的唯一有效值issue_status'OPEN'AND 'CLOSED'。如果不是这种情况——并且如果你想要计算其他状态——那么相应地调整查询。

于 2012-06-11T14:47:48.207 回答
4

另一种方法是使用新的 PIVOT 功能:

with issue_data as (
  select associated_project as project, issue_status from issues
)
select project, open, closed
  from issue_data
       pivot ( count(*) for issue_status in ('OPEN' as open, 'CLOSED' as closed) )
于 2012-06-11T14:58:14.170 回答
1
select
sum(case when status = 'open' then 1 else 0 end) as open,
sum(case when status = 'closed' then 1 else 0 end) as closed
from table
where  <other restrictions>
于 2012-06-11T14:48:55.627 回答