我有一个查询返回两个表中的count和一些列,另一个查询返回两个表中的count和一列。 我想结合这两个查询,每个id导致单行。 我试过这个:
select
(select a.column_1 as ID,a.column_2,COUNT(b.column_2) as Cnt1
from
table_1 a left outer join table_2 b on a.ID=b.ID
group by
a.column_1 as ID,a.column_2
)
where EXISTS
(select a.column_1 as ID,COUNT(c.column_2) as Cnt2
from
table_1 a left outer join table_3 c on a.ID=c.ID
group by
a.column_1
)
答案 0 :(得分:0)
在不知道您的真实架构的情况下...基于您的示例查询,假设您的内部查询是正确的(一些小的语法错误)。你可能想做这样的事情。
select *
FROM
(select a.column_1 as ID,a.column_2,COUNT(b.column_2) as Cnt1
from table_1 a left outer join table_2 b on a.ID=b.ID
group by a.column_1,a.column_2
) T1
INNER JOIN
(select a.column_1 as ID,COUNT(c.column_2) as Cnt2
from table_1 a left outer join table_3 c on a.ID=c.ID
group by a.column_1
) T2 ON T1.ID = T2.ID
您可能希望根据评论将select *
替换为select T1.ID,T1.column_2,Cnt1,Cnt2
。