我有以下3个查询: -
SELECT to_char(Actual_SSD,'YYYY-MM') AS Period, Count(Actual_SSD)
FROM tblQuotesNew
where tblQuotesNew.Quote_Status='Accepted'
group by to_char(Actual_SSD,'YYYY-MM')
order by to_char(Actual_SSD,'YYYY-MM')
SELECT to_char(requested_SSD,'YYYY-MM') AS Period, Count(requested_SSD)
FROM tblQuotesNew
where tblQuotesNew.Quote_Status='Accepted'
group by to_char(requested_SSD,'YYYY-MM')
order by to_char(requested_SSD,'YYYY-MM')
SELECT to_char(date_sent_to_registrations,'YYYY-MM') AS Period, Count(date_sent_to_registrations)
FROM tblQuotesNew
where tblQuotesNew.Quote_Status='Accepted'
group by to_char(date_sent_to_registrations,'YYYY-MM')
order by to_char(date_sent_to_registrations,'YYYY-MM')
Oracle是否有办法将这些方法合并为一个包含4列的查询,最左边的列是日期,接下来的3列是计数。在Access中我会使用交叉表查询执行此操作,但我不确定它是否可以在Oracle
中使用答案 0 :(得分:1)
这是一种做法。
select coalesce ( act.period, req.period, reg.period) as period
, act.act_count
, req.req_count
, reg.reg_count
from (SELECT to_char(Actual_SSD,'YYYY-MM') AS Period,
Count(Actual_SSD) as act_count
FROM tblQuotesNew
where tblQuotesNew.Quote_Status='Accepted'
group by to_char(Actual_SSD,'YYYY-MM')) act
full outer join
( SELECT to_char(requested_SSD,'YYYY-MM') AS Period,
Count(requested_SSD) as req_count
FROM tblQuotesNew
where tblQuotesNew.Quote_Status='Accepted'
group by to_char(requested_SSD,'YYYY-MM')) req
on req.period = act.period
full outer join
( SELECT to_char(date_sent_to_registrations,'YYYY-MM') AS Period,
Count(date_sent_to_registrations) as reg_count
FROM tblQuotesNew
where tblQuotesNew.Quote_Status='Accepted'
group by to_char(date_sent_to_registrations,'YYYY-MM') ) reg
on reg.period = act.period
order by 1
这接近您现有的逻辑,因此应该重现从单独查询中获得的结果。
完整的外部联接可以处理在某个月内不是每种类型的事件都发生的情况。 coalesce()
函数从子查询中获取第一个非空period
。