Sql 选择“最大值”,并从该行获取其他数据

Sql 选择“最大值”,并从该行获取其他数据,sql,join,max,Sql,Join,Max,我正试图找到物品库存最多的卖家,并显示该卖家以及该行中的所有其他物品。出于某种原因,我的查询一直在拉动整个表 SELECT DISTINCT Name, Username, Password, Address, Email, CCNumber, CCExpiration, ItemsSold FROM auctionsite.dbo.Seller JOIN (SELECT MAX(ItemsSold) AS id FROM auctionsite.

我正试图找到物品库存最多的卖家,并显示该卖家以及该行中的所有其他物品。出于某种原因,我的查询一直在拉动整个表

SELECT DISTINCT
   Name,
   Username,
   Password,
   Address,
   Email,
   CCNumber,
   CCExpiration,
   ItemsSold
FROM auctionsite.dbo.Seller
JOIN (SELECT MAX(ItemsSold) AS id FROM auctionsite.dbo.Seller GROUP BY 
   ItemsSold) max ON id = max.id
您可以使用order by和逻辑获取一行:

select s.*
from auctionsite.dbo.Seller s
order by s.itemSold desc
fetch first 1 row only;
注意:如果存在绑定,则只获取一个绑定行

如果需要所有关系,有一种方法使用子查询:

select s.*
from auctionsite.dbo.Seller s
where s.itemSold = (select max(s2.itemSold) from auctionsite.dbo.Seller s2);