SQL: Select Top 3 Records + Sum of Quantity

后端 未结 2 1466
广开言路
广开言路 2020-12-18 06:39

I would like to display the top 3 records from the existing Orders table. In order to accomplish this, I need to calculate the sum of each product\'s quanti

2条回答
  •  清歌不尽
    2020-12-18 06:55

    You need to SUM and then ORDER BY this summary value:

    SELECT TOP 3 ProductID, SUM(Quantity) as qSum
    FROM Table
    GROUP BY ProductID
    ORDER BY qSum DESC
    

提交回复
热议问题