Ever wanted to query a table and get all records that have one of the top 3 values? Let’s say you have a Sales-table where all your sales are stored. You want to find the top 3 sales, but you want to include “ties”. Let me show you with code.
DECLARE @Sales TABLE (
Customer NVARCHAR(200),
TotalSum FLOAT
)
INSERT INTO @Sales
(Customer, TotalSum)
VALUES
('Statoil', 4000000),
('Hydro', 1000000),
('Kiwi', 100000),
('Mega', 100000),
('Ølen IL', 20)
SELECT TOP (3) WITH TIES
*
FROM @Sales
ORDER BY TotalSum DESC
The results will be 4 rows:
| Customer |
TotalSum |
| Statoil |
4000000 |
| Hydro |
1000000 |
| Kiwi |
100000 |
| Mega |
100000 |
Pretty neat?