Selecting records from column

Losbaltica

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 :(

Roger Russel

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

Results:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Oracle Query - Selecting records using column data from first table

From Dev

Selecting records from a table and then selecting a count of records from another table

From Dev

Selecting correlated records from database

From Dev

Selecting records from database mysql

From Dev

Selecting from table into a column

From Dev

Selecting records matching ONLY/AND criteria on one column

From Dev

Selecting Grouped Records based on max value in a column

From Dev

Selecting all records from two different tables

From Dev

Selecting top 3 distinct records from a table

From Dev

Selecting records from a table not in another table

From Dev

selecting elements from a column in Excel

From Dev

Selecting varchar values from a column

From Dev

creating unique new column id by selecting first 7 characters from a column X and inserting into the new column Y for 10 000 records within same table

From Dev

Selecting Column Slice from Cassandra using CQL

From Dev

Selecting distinct value from a column in MySql

From Dev

selecting a specific part of a column from a database(oracle)

From Dev

Selecting distinct values from a column in Peewee

From Dev

Selecting rows from a table depending on a column

From Dev

Selecting from database using SQLite - 'No such column'

From Dev

selecting one column from 3 different tables

From Dev

selecting a specific part of a column from a database(oracle)

From Dev

Randomly selecting a row for a specific column from CSV

From Dev

Pandas: Selecting column from data frame

From Dev

Selecting from database using SQLite - 'No such column'

From Dev

Selecting columns from database if column exist

From Dev

Selecting maximum date from an XML column

From Dev

Selecting a column in excel but from the desired position

From Dev

Selecting particular values from a column in a dataframe

From Dev

Selecting multiple rows from a dataframe using a column

Related Related

  1. 1

    Oracle Query - Selecting records using column data from first table

  2. 2

    Selecting records from a table and then selecting a count of records from another table

  3. 3

    Selecting correlated records from database

  4. 4

    Selecting records from database mysql

  5. 5

    Selecting from table into a column

  6. 6

    Selecting records matching ONLY/AND criteria on one column

  7. 7

    Selecting Grouped Records based on max value in a column

  8. 8

    Selecting all records from two different tables

  9. 9

    Selecting top 3 distinct records from a table

  10. 10

    Selecting records from a table not in another table

  11. 11

    selecting elements from a column in Excel

  12. 12

    Selecting varchar values from a column

  13. 13

    creating unique new column id by selecting first 7 characters from a column X and inserting into the new column Y for 10 000 records within same table

  14. 14

    Selecting Column Slice from Cassandra using CQL

  15. 15

    Selecting distinct value from a column in MySql

  16. 16

    selecting a specific part of a column from a database(oracle)

  17. 17

    Selecting distinct values from a column in Peewee

  18. 18

    Selecting rows from a table depending on a column

  19. 19

    Selecting from database using SQLite - 'No such column'

  20. 20

    selecting one column from 3 different tables

  21. 21

    selecting a specific part of a column from a database(oracle)

  22. 22

    Randomly selecting a row for a specific column from CSV

  23. 23

    Pandas: Selecting column from data frame

  24. 24

    Selecting from database using SQLite - 'No such column'

  25. 25

    Selecting columns from database if column exist

  26. 26

    Selecting maximum date from an XML column

  27. 27

    Selecting a column in excel but from the desired position

  28. 28

    Selecting particular values from a column in a dataframe

  29. 29

    Selecting multiple rows from a dataframe using a column

HotTag

Archive