SQL: Select Top 3 Records + Sum of Quantity

后端 未结 2 1457
广开言路
广开言路 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:48
    SELECT TOP 3 ProductID, SUM(Quantity) as SUMQUANTITY
    FROM Table1
    GROUP BY ProductID
    ORDER BY SUMQUANTITY desc
    

    SQL Fiddle Demo

    0 讨论(0)
  • 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
    
    0 讨论(0)
提交回复
热议问题