holyredbeard
holyredbeard

Reputation: 21288

Showing top 10 results in a table (SQL Server)

I need to be able to show the items that has the top 10 highest values (quantity*price). In MySQL you can use LIMIT, but that's not possible in SQL Server. How can I achieve my goal?

Thanks in advance

SELECT ItemID, Itemname, Quantity, Price,
CONVERT(Decimal(8,0),ROUND((Quantity*price),2)) AS Total
FROM Item

Upvotes: 0

Views: 1251

Answers (4)

apocalypse
apocalypse

Reputation: 5884

SELECT TOP 10 ItemID, ...

Maybe this will help you? Also look BOTTOM keyword.

Upvotes: 1

Hauke Ingmar Schmidt
Hauke Ingmar Schmidt

Reputation: 11606

You would use the TOP clause.

Upvotes: 1

Ryan
Ryan

Reputation: 28247

SELECT TOP 10 ItemID, Itemname, Quantity, Price,
CONVERT(Decimal(8,0),ROUND((Quantity*price),2)) AS Total
FROM Item
ORDER BY Quantity * Price DESC

The ORDER BY Quantity * Price DESC will ensure that the highest values are returned first.

Upvotes: 7

Bruno Silva
Bruno Silva

Reputation: 3097

SELECT TOP 10 TItemID, Itemname, Quantity, Price,
CONVERT(Decimal(8,0),ROUND((Quantity*price),2)) AS Total
FROM Item
ORDER BY Total DESC

Upvotes: 4

Related Questions