在Oracle中并排加入两个查询结果列

时间:2016-03-27 11:21:57

标签: sql database oracle

我从同一个表中写了两个查询,但我需要并排查看结果,我已经尝试了但是收到了错误

第一次查询 -

select trunc(CONFIRM_DATE) RE_REG_DATE, to_char(CONFIRM_DATE,'HH24') as "ret_req_Hour", count(*) as Total_H_Hour
from MYTABLE
where trunc(CONFIRM_DATE) between'26-MAR-2016' and '26-MAR-2016'
and TO_char(CONFIRM_DATE,'HH24') in ('10','11','12','13','16','17','18','19')
group by trunc(CONFIRM_DATE), to_char(CONFIRM_DATE,'HH24')

第二次查询

select to_char(CONFIRM_DATE,'HH24') as "ret_req_Hour", count(*) as Hourly_S
from MYTABLE
where trunc(CONFIRM_DATE) between'26-MAR-2016' and '26-MAR-2016'
and TO_char(CONFIRM_DATE,'HH24') in ('10','11','12','13','16','17','18','19')
and status in ('C','Y','E')
group by trunc(CONFIRM_DATE), to_char(CONFIRM_DATE,'HH24')
order by trunc(CONFIRM_DATE),to_char(CONFIRM_DATE,'HH24');

1 个答案:

答案 0 :(得分:0)

只需使用条件聚合:

select trunc(CONFIRM_DATE) as RE_REG_DATE,
       to_char(CONFIRM_DATE, 'HH24') as ret_req_Hour,
       count(*) as Total_H_Hour,
       sum(case when status in ('C', 'Y', 'E') then 1 else 0 end) as Hourly_S
from MYTABLE
where trunc(CONFIRM_DATE) between '26-MAR-2016' and '26-MAR-2016' and
      TO_char(CONFIRM_DATE, 'HH24') in ('10', '11', '12', '13', '16', '17', '18', '19')
group by trunc(CONFIRM_DATE), to_char(CONFIRM_DATE,'HH24')