如何将这两个语句合并到一个子 select

How Can I Combine These Two Statements in a sub select

期望的输出:

用户 -- 关闭 -- 打开

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 共享相同的登录名。

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

完全外部联接查询以获取所有用户,即使他们仅出现在两个查询之一中也是如此:

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是关键字,如果有问题,重命名。)

如果您想显示两个查询中都不存在的用户,您需要一个用户 table 到 select 来自:

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;