Oracle SQL。基于两列计数

时间:2016-04-12 06:30:38

标签: sql oracle count

我有一张桌子:

JIRA_KEY   DATACENTER STATUS 
  1         US        CLOSED
  2         EM        CLOSED
  3         AP        CLOSED
  4         US        CLOSED
  5         EM        IN PROGRESS
  6         AP        CANCELLED
  7         US        IN PROGRESS
  8         US        CANCELLED  
  9         AP        CANCELLED  
 10         AP        IN PROGRESS

预期产出:

Datacenter TotalJiras Closed InProgress Cancelled
EMEA          2       1        1           0
APAC          4       1        1           2
AMERICA       4       2        1           1

我尝试创建一个视图:

create or replace view VW_JIRA_STATUS_BK AS 
SELECT count(JIRA_KEY) JIRA ,
  decode (substr(data_center,1,2),'EM', 'EMEA', 'AP', 'APAC', 'US', 'AMERICA') as REGION, 
  status 
from hw_jira_status 
group by data_center , status;

select * from VW_JIRA_STATUS_BK

不会按预期显示结果。

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:2)

您需要使用CASE 条件聚合,每个结果列一个

public static void main(String[] args) {
        String s1 = "hello.world";
        String[] s2 = s1.split("\\.");
        System.out.println(Arrays.toString(s2));
}

为什么如果它是create or replace view VW_JIRA_STATUS_BK AS SELECT decode (substr(data_center,1,2),'EM', 'EMEA', 'AP', 'APAC', 'US', 'AMERICA') as REGION, count(JIRA_KEY) AS TotalJiras, COUNT(CASE WHEN Status = 'CLOSED' THEN 1 END) AS Closed, COUNT(CASE WHEN Status = 'IN PROGRESS' THEN 1 END) AS InProgress, COUNT(CASE WHEN Status = 'CANCELLED' THEN 1 END) AS Cancelled from hw_jira_status group by data_center; -- don't group by "Status" ,如果它是两个字符的话,你会在SUBSTR上申请?

顺便说一句,我认为标准SQL data_center比Oracle已弃用CASE

DECODE