将3个单独的sql查询组合成一个结果

时间:2013-06-13 00:11:58

标签: sql tsql

所以我有三个sql查询请求3个不同的东西 但我希望能够将它们组合成1个表格,以便每个查询出现在每一列

这些是查询:

select b.name, count(*)
from account a join branch b
on a.open_branch_id = b.branch_id
group by b.name;

select b.name, count(*)
from employee e join branch b
on e.assigned_branch_id = b.branch_id
group by b.name;

select b.name, count(*)
from customer c join branch b
on c.city = b.city
group by b.name;

我不知道如何组合它们,因此查询1将出现在1列中,查询2出现在第2列中,查询3出现在第3列中。

有人有什么想法吗?

由于

4 个答案:

答案 0 :(得分:0)

如果我了解您的问题,您想根据每个查询中的姓名字段加入结果吗?假设每个名称都相同,则可以使用JOINs和子查询执行此操作。这是一个简化版本(用上面的子查询替换):

select t1.name, t1.cnt, 
       t2.name as t2name, t2.cnt as t2cnt, 
       t3.name as t3name, t3.cnt as t3cnt
from 
  (select name, count(1) cnt
   from t1
   group by name) t1 join 
  (select name, count(1) cnt
   from t2
   group by name) t2 on t1.name = t2.name join 
  (select name, count(1) cnt
   from t3
   group by name) t3 on t1.name = t3.name   

编辑,鉴于您的评论,这似乎是您可能正在寻找的:

select b.name, 
  a.acctcnt, e.empcnt, c.citycnt
from branch b
  left join (
    select count(1) acctcnt, open_branch_id
    from account 
    group by open_branch_id
    ) a on a.open_branch_id = b.branch_id 
  left join (
    select count(1) empcnt, assigned_branch_id
    from employee
    group by assigned_branch_id
    ) e on e.assigned_branch_id = b.branch_id 
  left join (
    select count(1) citycnt, city
    from customer 
    group by city
    ) c on c.city = b.city

答案 1 :(得分:0)

你可以这样做(SQL Server 2005及以上版本):

SELECT
   B.Name,
   B.OtherColumnsIfYouLike,
   ACount = IsNull(ACount, 0),
   ECount = IsNull(E.ECount, 0),
   CCount = IsNull(C.CCount, 0)
FROM
   dbo.Branch B
   OUTER APPLY (
      SELECT ACount = Count(*)
      FROM dbo.Account A
      WHERE B.Branch_ID = A.Open_Branch_ID
   ) A
   OUTER APPLY (
      SELECT ECount = Count(*)
      FROM dbo.Employee E
      WHERE B.Branch_ID = E.Assigned_Branch_ID
   ) E
   OUTER APPLY (
      SELECT CCount = Count(*)
      FROM dbo.Customer C
      WHERE B.City = C.City
   ) C
;

您使用的任何查询都需要注意该分支名称是否可能没有任何结果。使用INNER JOIN(或仅JOIN)将排除这些,而不是正确显示0

完整地使用现有查询将如下所示,但效果不佳:

SELECT
   Name = Coalesce(A.Name, E.Name, C.Name),
   ACount = IsNull(ACount, 0),
   ECount = IsNull(E.ECount, 0),
   CCount = IsNull(C.CCount, 0)
FROM
   (
      select b.name, ACount = Count(*)
      from account a join branch b on a.open_branch_id = b.branch_id
      group by b.name
   ) A
   FULL JOIN (
      select b.name, ECount = Count(*)
      from employee e join branch b on e.assigned_branch_id = b.branch_id
      group by b.name
   ) E ON A.Name = E.Name
   FULL JOIN (
      select b.name, CCount = Count(*)
      from customer c join branch b on c.city = b.city group by b.name
   ) C ON IsNull(A.Name, E.Name) = C.Name
;

答案 2 :(得分:0)

如果name列具有相同的值,您可以像这样加入它们。但是,从您的查询来看,您展示的名称似乎是分支机构,员工和客户。

select b.name as Name, count(*) as [Count] into #Result1 from account a join branch b on a.open_branch_id = b.branch_id group by b.name;

select b.name  as Name, count(*) as [Count] into #Result2 from employee e join branch b on e.assigned_branch_id = b.branch_id group by b.name;

select b.name  as Name, count(*) as [Count] into #Result3 from customer c join branch b on c.city = b.city group by b.name;

select r1.name,
    r1.[Count],
    r2.[Count],
    r3.[Count],
from #Result1 as r1
        left join #Result2 as r2 on r1.name = r2.name
        left join #Result3 as r3 on r1.name = r3.name

drop table #Result1
drop table #Result2
drop table #Result3

答案 3 :(得分:0)

我认为这是最快的:

with a as
(
  select open_branch_id as b_id, count(*) as a_count
  from account a 
  group by open_branch_id 
), e as
(
  select assigned_branch_id as b_id, count(*) as e_count
  from employee e 
  group by assigned_branch_id
), c as
(
  select b.branch_id as b_id, count(*) as c_count
  from customer c join branch b
  on c.city = b.city
  group by b.name
)
select b.name, a.a_count, e.e_count, c.c_count
from branch b
left join a on a.b_id = b.branch_id 
left join e on e.b_id = b.branch_id 
left join c on c.b_id = b.branch_id 
order by b.name