SQL: Select Top 3 Records + Sum of Quantity
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
SQL - select top 10 rows with the highest number (counted)
You need to SUM
number field and order by it like this;
SELECT TOP 10
playerID,
SUM([number]) as goals,
[league],
[yearID],
SUM([YellowCard]) as YellowCards,
SUM([RedCard]) as RedCards,
FROM [ms4033].[dbo].[Shooter]
WHERE yearID=28 AND league=4
GROUP BY playerID, league, yearID
ORDER BY goals DESC
How to SELECT top N rows that sum to a certain amount?
One approach:
select t1.amount
from MyTable t1
left join MyTable t2 on t1.amount > t2.amount
group by t1.amount
having coalesce(sum(t2.amount),0) < 7
SQLFiddle here.
Related Topics
Sql: Performance Comparison for Exclusion (Join VS Not In)
Rounding to 2 Decimal Places in SQL
Add a Column That Represents a Concatenation of Two Other Varchar Columns
Many-To-Many Relations in Rdbms Databases
Split String in SQL Server to a Maximum Length, Returning Each as a Row
Sql: Try/Catch Doesn't Catch an Error When Attempting to Access a Table That It Can't Find
Postgresql Error: Function To_Tsvector(Character Varying, Unknown) Does Not Exist
Ansi SQL Version of Select Top 1
MySQL Error 1248 (42000): Every Derived Table Must Have Its Own Alias
How to Know How Many Rows Will Be Affected Before Running a Query in Microsoft SQL Server 2008
Postgres Syntax Error at or Near "On"
What Is the Equivalent of Xml Path and Stuff in Linq Lambda Expression (Group_Concat/String_Agg)
Delphi: How to Pass a List as a Parameter to a SQL Query
How to Deep Copy a Set of Data, and Change Fk References to Point to All the Copies
What's the Best Practice of Naming Stored Procedure for T-Sql
Like and Null in Where Clause in SQL
Find Start and Stop Date for Contiguous Dates in Multiple Rows