对多个子查询表使用count(*)函数

时间:2019-04-05 03:34:09

标签: sql sql-server count subquery

我的任务是查找以下内容:“就安装在计算机上的软件包的总成本而言,有多少员工使用最昂贵的计算机?”

我能够使用以下方法找到与最昂贵的计算机关联的员工的主键:

With table3 as  
(select comp from PC where tagnum in
(select tagnum from 
(select tagnum, sum(softcost)'Totalcost' from software
group by tagnum) as Table1
where Totalcost = 
(select max(Totalcost)'MaxTotal' 
from
(select tagnum, sum(softcost)'Totalcost' 
from software
group by tagnum
) as Table2
)))
select empnum from PC 
where PC.comp in
 (select comp from table3 where PC.comp = table3.comp)

但是,根据这些结果,我无法找到该员工的人数。知道我该怎么做吗?

我在下面附加了我的数据... enter image description here

1 个答案:

答案 0 :(得分:0)

如前所述,如果查询运行良好。那么您可以使用不同的计数来获取正在使用该计算机的员工人数。

With table3 as  
(select comp from PC where tagnum in
(select tagnum from 
(select tagnum, sum(softcost)'Totalcost' from software
group by tagnum) as Table1
where Totalcost = 
(select max(Totalcost)'MaxTotal' 
from
(select tagnum, sum(softcost)'Totalcost' 
from software
group by tagnum
) as Table2
)))
select COUNT( Distinct empnum) from PC 
where PC.comp in
 (select comp from table3 where PC.comp = table3.comp)