简体   繁体   English

从列中选择记录

[英]Selecting records from column

I have to select 100 highest records by the number from column. 我必须通过列中的数字选择100条最高的记录 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 我需要为每个Nr B选择A列中的最高数字。在本示例中,它将是

---------
| A | B |
---------
| 31| 1 |
| 41| 2 |
---------

B1 = 31, 1; B1 = 31,1; B2 = 41, 22. 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. 这个任务看起来很简单,但是我在A列中有超过1000万个数字,在B列中有40 000 nr。

Can you please help me? 你能帮我么? I'm not really good at sql and script building :( 我不太擅长sql和脚本构建:(

I have a similar problem into a project that I made. 我在做的项目中也遇到类似的问题。

You should correct the tag MySql to SQLSERVER . 您应该将标记MySql更正为SQLSERVER

I use this SQL Fiddle to make a fiddle about the problem, I think that is what you want. 我使用此SQL提琴来对问题进行提琴,我想这就是您想要的。

MS SQL Server 2014 Schema Setup : MS SQL Server 2014架构设置

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 : 查询1

SELECT TOP 100 Max(a) as a, b
FROM tab1
GROUP BY b
ORDER BY b asc

Results : 结果

|  a | b |
    |----|---|
    | 31 | 1 |
    | 41 | 2 |
    | 11 | 3 |

As far as i understood, I guess this could help 据我了解,我想这可能会有所帮助

Select Top 10 A , B from tableName group by B , A

this will give you the following 这将为您提供以下内容

---------
| A | B |
---------
| 1 | 1 |
| 31| 1 |
| 22| 2 |
| 41| 2 |
---------

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM