从子查询返回多个列时如何只使用一列?

时间:2014-03-03 07:17:17

标签: sql subquery

假设我有一个像

这样的子查询
select deptNo, count(*) 
from employee group by deptNo

我想在主查询中使用count(*),我该怎么做?

例如:count(*) in (select deptNo, count(*) from employee group by deptNo)之类的查询; 或者做什么的替代方案?

2 个答案:

答案 0 :(得分:0)

select avg(cnt) 
from (
    select deptNo, count(*) as cnt
    from employee group by deptNo) src

答案 1 :(得分:0)

select t.count1  from 
(select deptNo,count(*) as count1 from employee group by deptNo) as t

尝试在内部查询中使用别名来计算。我假设您只需要列count1。(我已将列命名为count1)