3

我有以下两个表格,我需要将它们合并到我的每日报告中。

表 1:Resource_Created

FirstName LastName ObjDate 资源登录

TestDemo1        TestDemo1       5-Oct-12         AD         TESTDEMO1

表 2:Resource_Deleted

FirstName LastName ObjDate 资源登录

TestDemo4        TestDemo4       5-Oct-12         AD         TESTDEMO4

TestDemo5        TestDemo5       5-Oct-12         AD         TESTDEMO5

TestDemo6        TestDemo6       5-Oct-12         AD         TESTDEMO6

TestDemo4        TestDemo4       5-Oct-12         Bio        TESTDEMO4

TestDemo4        TestDemo4       5-Oct-12         VPN        TESTDEMO4

TestDemo5        TestDemo5       5-Oct-12         VPN        TESTDEMO5

TestDemo6        TestDemo6       5-Oct-12         VPN          TESTDEMO6

我分别写了两个查询,例如

查询一:

select distinct Resource as Resource, 
       count (distinct Login) as CountRes 
from Resource_Created 
where ObjDate between '4-Oct-12' and '6-Oct-12' 
group by Resource ;

结果:

资源计数资源

 AD        1

查询 2:

select distinct Resource as Resource, 
       count (distinct Login) as CountRes 
from Resource_Deleted 
where ObjDate between '4-Oct-12' and '6-Oct-12' 
group by Resource ;

结果

资源计数资源

AD          3

VPN         3

Bio         1

我希望将这两个查询结合起来,这样我就可以让一张表显示这些值。

select COALESCE (Resource_Created.Resource, Resource_Deleted.Resource) as Resource , 
 count (distinct Resource_Created.usrlogin) as aobj, 
 count (distinct Resource_Deleted.usrlogin) as bobj
FROM target_failed  FULL OUTER JOIN target_resource  
 on Resource_Created.Resource = Resource_Deleted.Resource 
where
 Resource_Created.ObjDate between '04-OCT-2012' and '06-OCT-2012' and 
 Resource_Deleted.ObjDate between '04-OCT-2012' and '06-OCT-2012'
group by COALESCE(Resource_Created.Resource, Resource_Deleted.Resource);

我的结果是

**Resource       aobj         bobj**

   AD         1                3

预期结果

资源 aobj bobj

 AD             1          3

VPN           Null         3

Bio           Null         1

请问谁能帮我解决这个问题。我只是一个编写基本 sql 查询的 OO 开发人员。这将不胜感激。

4

1 回答 1

2

只需在 from 语句上使用 sub sql

select 
  ResourceI as rs, sum (CreatedLogin) as CountCreated,
 sum (DeletedLogin) as CountDeleted
from 
(select 
 ObjDate,
 Resource ,
 1 as DeletedLogin,
 0 as CreatedLogin 
 from  Resource_Deleted
 union all
 select ObjDate,
 Resource ,
 0 as DeletedLogin,
 1 as CreatedLogin  
 from  Resource_Created
)  TABLE_ALL
where ObjDate between TO_DATE('4-Oct-12') and TO_DATE('6-Oct-12')
group by Resource
于 2012-10-14T10:09:59.093 回答