使用UNION ALL和GROUP BY进行SQL查询

时间:2014-02-12 18:50:47

标签: sql oracle

我想让结果像

**PROTOCOL_NAME GREEN   AMBER   RED**
DP1               1       1       1

但我得到的结果如下:

**PROTOCOL_NAME  STATUS    CNT**
DP1            GREEN      1
DP1            AMBER      1
DP1            RED        1

请帮助您更正查询?

select protocol_name,status,sum(qty) from 
(select mp.protocol_name, teh.status as status, count(mp.protocol_id) as qty
from t_execution_hourly teh, m_protocol mp, m_protocol_category mpc, m_service ms, m_domain md, m_environment me
where ms.service_id = 3
and md.domain_name = 'FlexA'
and me.environment_name = 'PROD'
and mpc.protocol_category in ('F5','DP','TOMCAT')
and me.environment_id = mp.environment_id
and md.domain_id= mp.domain_id
and ms.service_id = mp.service_id
and mpc.protocol_category_id = mp.protocol_category_id
and mp.protocol_id = teh.protocol_id
and teh.status = 'GREEN' 
and to_date(teh.EXECUTION_DATE) = to_date(sysdate)--between sysdate and (sysdate - 1)
group by  teh.status,mp.protocol_name

union all 

select mp.protocol_name, teh.status as status, count(mp.protocol_id) as qty
from t_execution_hourly teh, m_protocol mp, m_protocol_category mpc, m_service ms, m_domain md, m_environment me
where ms.service_id = 3
and md.domain_name = 'FlexA'
and me.environment_name = 'PROD'
and mpc.protocol_category in ('F5','DP','TOMCAT')
and me.environment_id = mp.environment_id
and md.domain_id= mp.domain_id
and ms.service_id = mp.service_id
and mpc.protocol_category_id = mp.protocol_category_id
and mp.protocol_id = teh.protocol_id
and teh.status = 'AMBER' 
and to_date(teh.EXECUTION_DATE) = to_date(sysdate)--between sysdate and (sysdate - 1)
group by  teh.status, mp.protocol_name

union all

select mp.protocol_name, teh.status as status, count(mp.protocol_id) as qty
from t_execution_hourly teh, m_protocol mp, m_protocol_category mpc, m_service ms, m_domain md, m_environment me
where ms.service_id = 3
and md.domain_name = 'FlexA'
and me.environment_name = 'PROD'
and mpc.protocol_category in ('F5','DP','TOMCAT')
and me.environment_id = mp.environment_id
and md.domain_id= mp.domain_id
and ms.service_id = mp.service_id
and mpc.protocol_category_id = mp.protocol_category_id
and mp.protocol_id = teh.protocol_id  
and teh.status = 'RED' 
and to_date(teh.EXECUTION_DATE) = to_date(sysdate)--between sysdate and (sysdate - 1)
group by  teh.status, mp.protocol_name
)t
group by status,protocol_name

2 个答案:

答案 0 :(得分:2)

如果我正确阅读,您的查询会过于复杂。你不需要将这些“联合”在一起。您可以选择所有这些并使用group by。试试这个:

select mp.protocol_name, 
       sum(case when status = 'GREEN' then 1 else 0 end) as green,
       sum(case when status = 'AMBER' then 1 else 0 end) as amber,
       sum(case when status = 'RED' then 1 else 0 end) as red
from t_execution_hourly teh, m_protocol mp, m_protocol_category mpc, m_service ms, m_domain md, m_environment me
where ms.service_id = 3
and md.domain_name = 'FlexA'
and me.environment_name = 'PROD'
and mpc.protocol_category in ('F5','DP','TOMCAT')
and me.environment_id = mp.environment_id
and md.domain_id= mp.domain_id
and ms.service_id = mp.service_id
and mpc.protocol_category_id = mp.protocol_category_id
and mp.protocol_id = teh.protocol_id  
and teh.status in ('AMBER', 'RED', 'GREEN'
and to_date(teh.EXECUTION_DATE) = to_date(sysdate)--between sysdate and (sysdate - 1)
group by mp.protocol_name;

您还应该学习使用现代连接语法,使用on子句。但这是一个不同的问题。

答案 1 :(得分:0)

您的结果正是您所要求的。

select protocol_name,status,sum(qty)

要获得所需内容,您需要进行不同的查询。获得所需内容的最短路径是将查询分为三个部分,让我们按状态调用它们。然后使用子查询代替联合:

select 
  (select count(mp.protocol_id) ... where status = 'GREEN') as green, 
  (select count(mp.protocol_id) ... where status = 'AMBER') as amber, 
  (select count(mp.protocol_id) ... where status = 'RED') as red