I have to select 100 highest records by the number from column. Structure looks like that:
--------- | A | B | --------- | 1 | 1 | | 22| 2 | | 31| 1 | | 41| 2 | ---------
I need to select for every nr B the highest numbers from column A. In this example it will be
--------- | A | B | --------- | 31| 1 | | 41| 2 | ---------
B1 = 31, 1; B2 = 41, 22.
The task looks quite easy, but I've got more than 10 mln numbers in column A and something like 40 000 nr in column B.
Can you please help me? I'm not really good at sql and script building :(
I have a similar problem into a project that I made.
You should correct the tag MySql to SQLSERVER.
I use this SQL Fiddle to make a fiddle about the problem, I think that is what you want.
MS SQL Server 2014 Schema Setup:
create table tab1 (
a int,
b int
);
insert into tab1 (a, b) values
(1,1),
(22,2),
(11,3),
(31,1),
(10,3),
(41,2);
Query 1:
SELECT TOP 100 Max(a) as a, b
FROM tab1
GROUP BY b
ORDER BY b asc
| a | b | |----|---| | 31 | 1 | | 41 | 2 | | 11 | 3 |
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments