I have a table with 3 columns, the data in column1 has repeating values and column 3 has totals, what I’d like to do is to return the top 2 totals for each value in column 1.
My query to create this table is below:
SELECT service,name, total
FROM [test].[dbo].[TestTable]
join test1.dbo.service
on substring(servan,0,4)=servicebn
where substring(servan,0,4)=servicebn and name <> testname
group by service,name,total
order by service,total desc
any help would be much appreciated
if you are using
SQL Server 2005+, you can use Common Table Expression and Window Function.As a side note, this query has poor in performance because it requires
FULL TABLE SCANon every table. The reason is because of the join conditionsubstring(servan,0,4)=servicebn. It doesn’t use index.