如何为每个CarID选择最高的BidAmount

时间:2019-01-30 09:34:19

标签: sql

我正在尝试从CarDetails表中选择数据,并且引用name City ID Country Type Gross PIT Mari Tbilisi 010080 Georgia 6 sum of Groos(if A:B:C:D:E are same ) 12 0 Mari Tbilisi 010080 Georgia 1 sum of Groos(if A:B:C:D:E are same ) 12 0 的BidDetails表具有多个引用同一辆车的“出价”。

CarID. BidDetails

选择的数据应该是每辆车的CarDetails表中的数据,以及每辆车的最高CarDetails Columns: CarID, Make,... BidDetails Columns: CarID, BidderID, Amount

但是我会选择每个出价以及相应的汽车信息。

代替:

bid

我得到:

CarID: 1 , Make: Toyota, BidAmount: 50000;
CarID: 2 , Make: Honda, BidAmount: 95000

2 个答案:

答案 0 :(得分:0)

您要查找简单的汇总吗?

SELECT CD.CarID, CD.Make, MAX(BD.BidAmount)
FROM CarDetails CD INNER JOIN
     BidDetails BD
     ON BD.CarID = CD.CarID
GROUP BY CD.CarID, CD.Make;

如果表中还有更多列,那么您需要关联的subquery

SELECT CD.CarID, CD.Make, BD.BidAmount
FROM CarDetails CD INNER JOIN
     BidDetails BD
     ON BD.CarID = CD.CarID
WHERE BD.BidAmount = (SELECT MAX(BD1.BidAmount) FROM BidDetails BD1 WHERE BD1.CarID = CD.CarID);

答案 1 :(得分:0)

您可以在下面尝试-

select carid, make,max(bidamount) as bidamount
from CarDetails CD INNER JOIN BidDetails BD
group by carid, make