0

所以我有三个 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 将出现在第三列中。

有人有想法么?

谢谢

4

4 回答 4

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
于 2013-06-13T00:35:35.873 回答
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
于 2013-06-13T00:20:57.930 回答
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
;
于 2013-06-13T00:21:40.677 回答
0

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

选择 b.name 作为名称,count(*) 作为 [Count] 从帐户 a 加入分支 b 在 a.open_branch_id = b.branch_id group by b.name 中的 #Result1;

选择 b.name 作为名称,count(*) 作为 [Count] 从员工 e 加入分支 b 在 e.assigned_branch_id = b.branch_id group by b.name 中的 #Result2;

选择 b.name 作为名称,count(*) 作为 [Count] 从客户 c 中选择 #Result3 加入 c.city 上的分支 b = b.city group by b.name;

选择 r1.name,
    r1.[计数],
    r2.[计数],
    r3.[计数],
从 #Result1 作为 r1
        left join #Result2 as r2 on r1.name = r2.name
        left join #Result3 as r3 on r1.name = r3.name

删除表 #Result1
删除表 #Result2
删除表 #Result3
于 2013-06-13T00:21:43.177 回答