CREATE TABLE Items
(
ItemId INT NOT NULL,
CategoryId INT NOT NULL,
ItemValue INT NOT NULL
)
A table contains items. Every item belongs to a category and stores some numeric value.
As a result of a query, I want each row to represent a category. There should be three columns:
- category id;
- id of an item with the highest
ItemValueper category; - id of an item with the lowest
ItemValueper category.
Performance of the query is important. I use MS SQL Server.
For SQL Server 2005+, you can do this:
Here is a fiddle where you can test this solution.