如何使用select语句和运算符从表中导出复杂数据

时间:2019-04-04 23:24:14

标签: sql sql-server select group-by operators

我有一个家庭作业问题,要求我“找出在PC上安装的软件总成本最高的PC”。

我已经可以使用以下语句找到最大的软件总成本

select max(Totalcost)'MaxTotal' from
(select tagnum, sum(softcost)'Totalcost' from software
group by tagnum) as Table1

但是,当我尝试选择与此max(Totalcost)相关的PC主键时,我一直遇到语法错误。如何选择与此最大值关联的PC主键?

任何帮助将不胜感激。我在下面的数据上附加了一个链接

enter image description here

1 个答案:

答案 0 :(得分:0)

如果有人好奇,我相信我使用以下方法找到了答案:

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 Table1
))