查询两个表(从第二个表的2列中获取最大值)

时间:2013-06-11 13:34:17

标签: sql sql-server sql-server-2008

我有两张桌子,

第一:汽车 ID(主),名称

第二名:已售出 ID(主),CarID,价钱,SoldUnits

我如何查询以获取汽车名称(Price * SoldUnits)最多?

1 个答案:

答案 0 :(得分:2)

寻找畅销车:

select  top 1 c.name
from    cars c
join    sold s
on      s.CarID = c.ID
group by
        c.Name
order by
        sum(Price * SoldUnits) desc

找到单一最大销售的汽车:

select  top 1 c.name
from    cars c
join    sold s
on      s.CarID = c.ID
order by
        Price * SoldUnits desc