选择最大值并从该行中获取其他数据

时间:2019-07-18 15:53:34

标签: sql join max

我试图找到“ ItemsSold”最多的卖家,并在该行中显示该卖家和其他所有商品。由于某种原因,我的查询不断拉动整个表。

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

1 个答案:

答案 0 :(得分:1)

您可以使用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);