在excel中生成查询结果

时间:2017-07-27 09:21:46

标签: sql excel oracle report oracle-sqldeveloper

目前我正在使用oracle报告在excel中生成每周报告。此报告背后有SQL查询。 oracle报表服务器大部分时间都处于关闭状态,我发现每周报告都不可靠。

所以我想自动化我的sql查询并在excel上生成报告,而不是复制查询结果并粘贴到excel中。我可以使用Procedure或PL / SQL块来执行此操作。但我不确定是否可以创建excel文件并使用PL / SQL生成报告.PL / SQL块或过程应该基于RESOURCE_ID coulmn参数化,因为我也可以对另一个资源使用相同的过程。我正在使用oracle sql developer工具编写oracle查询。

--Query 1  
select  db,db_date,count(distinct sales_id)  
from Sales_Order  
where  
db='Test'  
and resource_id=2 and  
db_date between 20170710 and 20170716  
group by db,db_date  

--Query 2  
select  db,db_date,count(distinct it_id)  
from IT_INFO  
where  
db='Test'  
and resource_id=2 and  
db_date between 20170710 and 20170716  
group by db,db_date  

我想生成excel文件的报告,如下所示:

enter image description here

1 个答案:

答案 0 :(得分:0)

不确定你想做什么,但你首先要做的就是加入2个查询:

Union All解决方案:

select  db,db_date,count(distinct sales_id) "SALES", "IT"
from Sales_Order  
where  
db='Test'  
and resource_id=2 and  
db_date between To_date('20170710','YYYYMMDD') and To_date('20170716','YYYYMMDD')
group by db,db_date  

UNION ALL

select  db,db_date, "SALES", count(distinct it_id)  "IT"
from IT_INFO  

where  
db='Test'  
and resource_id=2 and  
db_date between To_date('20170710','YYYYMMDD') and To_date('20170716','YYYYMMDD')
group by db,db_date  

您可以使用resource_id列加入2个表:

select  so.db,so.db_dat,count(distinct so.sales_id) "SALES", count(distinct t_i.it_id) "IT"
from Sales_Order so
left outer join IT_INFO it_i on so.resource_id = it_i.resource_id --if this is corresponding column
where  
so.db='Test'  
and so.resource_id=2 and  
so.db_date between To_date('20170710','YYYYMMDD') and To_date('20170716','YYYYMMDD')
group by so.db,so.db_date

可以从Oracle SQL Developer直接将数据导出到Excel:

单击鼠标右键>出口>下一个>完成

enter image description here