Unanswered: top 10 charges for each itemid (was "Need Simple Solution")
I've a table with two columns ITEMID and CHARGES.
Multiple ITEMIDs with different CHARGES exists. I want to query TOP 10 CHARGES for each ITEMID in the table; how come i achieve this without typing the TOP 10 for each ITEMID in the table. The table has two many itemids and i want to view 10 for each with maximum charges.
declare @N int
set @N = 5 --The number of records to return for each grouping.
inner join YourTable ThetaTable
on YourTable.GroupColumns = ThetaTable.GroupColumns
and YourTable.SortColumn <= ThetaTable.SortColumn
group by YourTable.YourColumns
having count(*) <= @N
If it's not practically useful, then it's practically useless.