选择订单总数最高的买家名称

时间:2019-07-03 10:07:38

标签: sql sql-server

我有两个表:

  1. 买家:

enter image description here

  1. 订单:

enter image description here

我想获得订单数量最多的买家的姓名(在这种情况下,它将是ID为3的买家C)。

我写了以下查询:

select top 1 b.Name, sum (o.Amount) as amt from Buyers b
join Orders o on o.BuyerId = b.Id
group by Name
order by amt desc

它在结果行中给了我Name和amt。

enter image description here

如何从结果中排除 amt 并仅显示买方的名称?

1 个答案:

答案 0 :(得分:4)

从选择中删除sum (o.Amount) as amt并按以下顺序添加sum (o.Amount) desc

select top 1 b.Name from Buyers b
join Orders o on o.BuyerId = b.Id
group by Name
order by sum (o.Amount)  desc