期望的输出:
用户 - 已关闭 - 已打开
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”看他们的名字是最佳的。我假设需要一个案例陈述。
答案 0 :(得分: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;