按顺序t-sql排序

时间:2015-03-13 18:43:05

标签: sql sql-server tsql

select 
    w.firstName, w.lastName, w.fbId, q.userId, 
    SUM(q.bidCount) as sumOfBids
from 
    geopl_kubetiAuct.firstWeekBids as q 
join 
    geopl_kubetiAuct.Users w on q.userId = w.Id
where 
    q.bidedItem = 'laptop' 
group by 
    w.firstName, w.lastName, w.fbId, q.userId

这将选择满足此要求的所有用户。如何选择前2 sumOfBids

我在order by sumOfBids top 2 desc之上添加了group by,但它不起作用

1 个答案:

答案 0 :(得分:3)

SELECT TOP 2 w.firstName,
             w.lastName,
             w.fbId,
             q.userId,
             Sum(q.bidCount) AS sumOfBids
FROM   geopl_kubetiAuct.firstWeekBids AS q
       INNER JOIN geopl_kubetiAuct.Users w
               ON q.userId = w.Id
WHERE  q.bidedItem = 'laptop'
GROUP  BY w.firstName,
          w.lastName,
          w.fbId,
          q.userId
ORDER  BY sumOfBids DESC