在同一个表上获取不同ID的最高值?

时间:2011-05-13 13:20:21

标签: sql sql-server select sum

我有出价表,每个出价都有金额和AuctionID。

我希望SUM / SELECT每个AuctionID的所有最高出价..

示例:

结果:

SELECT AuctionID,Amount, Highest FROM Bids Where Burned=0 ORDER BY Amount DESC

AuctionID   Amount  Highest
   1       44.4400     0
   3       43.7800     0
   2       42.3300     0
   1       22.2200     0
   4       21.2700     0
   1       21.2600     0
   4       21.2500     0
   2       21.2400     0
   1       12.6600     0
   4       12.5200     0

应返回 44.44 43.78 42.33 21.27

'最高'是一个我认为可能有用的旗帜,它仍然毫无用处。 我想看看是否有一种方法可以不使用Flag。

3 个答案:

答案 0 :(得分:4)

一个简单的group by子句可以解决这个问题:

select AuctionID, MAX(Amount)
from table
group by AuctionID

答案 1 :(得分:4)

SELECT AuctionId, MAX(Amount) FROM TableName GROUP BY AuctionID

答案 2 :(得分:1)

获得所有最高出价:

Select auctionid, max(amount) from auctions group by auctionid

获取最高出价的总和:

select sum(v1.max_amount) from
(Select auctionid, max(amount) max_amount from auctions group by auctionid
) as v1