1

期望的输出:

用户 -- 关闭 -- 打开

Query 1: (Closed)
select AM1.SYSMODUSER, COUNT(AM1.SYSMODUSER)
from AuditLog AM1
where AM1.SYSMODTIME > '2015-05-01'
and AM1.SYSMODTIME < '2015-05-13'
and AM1.NUMBER like '%IM%'
and AM1.TYPE = 'Closed'
and (AM1.SYSMODUSER = 'login1'
or AM1.SYSMODUSER = 'login2')

Query 2: (Open)
select ASSIGNEE, count(record_id)
from List1
where "GROUP" = 'Records Compilation'
and RECORD_ID like '%IM%'
and ASSIGNEE is not null
group by ASSIGNEE

SYSMODUSER 和 ASSIGNEE 共享相同的登录名。

此外,如果可能的话,我希望它显示登录,即使它们的计数为空或零。目前,使用任一查询,它只返回实际计数。如果用户没有关闭或打开的任务,他们的名字甚至不会出现在结果集中。最好用“0”来查看他们的名字。我认为为此需要一个案例陈述。

4

1 回答 1

2

完全外连接查询以获取所有用户,即使他们只出现在两个查询之一中:

select 
  coalesce(open.userid, closed.userid) as userid, 
  coalesce(closed.cnt, 0) as closed, 
  coalesce(open.cnt, 0) as open
from
(
  select AM1.SYSMODUSER as userid, COUNT(AM1.SYSMODUSER) as cnt
  from AuditLog AM1
  where ...
  GROUP BY AM1.SYSMODUSER
) closed
full outer join
(
  select ASSIGNEE as userid, count(record_id) as cnt
  from List1
  where ...
  group by ASSIGNEE
) open on open.userid = closed.userid;

(也许open是一个关键字。如果你有问题,重命名它。)

如果您想显示两个查询中都不存在的用户,您需要一个用户表来选择:

select 
  user.id, 
  coalesce(closed.cnt, 0) as closed,
  coalesce(open.cnt, 0) as open
from user
left outer join (<query 1 here>) open on open.userid = user.id
left outer join (<query 2 here>) closed on closed.userid = user.id;
于 2015-05-13T14:49:50.113 回答