Select top 10 records for each category

前端 未结 14 1163
别那么骄傲
别那么骄傲 2020-11-22 04:27

I want to return top 10 records from each section in one query. Can anyone help with how to do it? Section is one of the columns in the table.

Database is SQL Serve

相关标签:
14条回答
  • 2020-11-22 05:16

    If you know what the sections are, you can do:

    select top 10 * from table where section=1
    union
    select top 10 * from table where section=2
    union
    select top 10 * from table where section=3
    
    0 讨论(0)
  • 2020-11-22 05:16

    While the question was about SQL Server 2005, most people have moved on and if they do find this question, what could be the preferred answer in other situations is one using CROSS APPLY as illustrated in this blog post.

    SELECT *
    FROM t
    CROSS APPLY (
      SELECT TOP 10 u.*
      FROM u
      WHERE u.t_id = t.t_id
      ORDER BY u.something DESC
    ) u
    

    This query involves 2 tables. The OP's query only involves 1 table, in case of which a window function based solution might be more efficient.

    0 讨论(0)
  • 2020-11-22 05:17

    In T-SQL, I would do:

    WITH TOPTEN AS (
        SELECT *, ROW_NUMBER() 
        over (
            PARTITION BY [group_by_field] 
            order by [prioritise_field]
        ) AS RowNo 
        FROM [table_name]
    )
    SELECT * FROM TOPTEN WHERE RowNo <= 10
    
    0 讨论(0)
  • 2020-11-22 05:21

    If you want to produce output grouped by section, displaying only the top n records from each section something like this:

    SECTION     SUBSECTION
    
    deer        American Elk/Wapiti
    deer        Chinese Water Deer
    dog         Cocker Spaniel
    dog         German Shephard
    horse       Appaloosa
    horse       Morgan
    

    ...then the following should work pretty generically with all SQL databases. If you want the top 10, just change the 2 to a 10 toward the end of the query.

    select
        x1.section
        , x1.subsection
    from example x1
    where
        (
        select count(*)
        from example x2
        where x2.section = x1.section
        and x2.subsection <= x1.subsection
        ) <= 2
    order by section, subsection;
    

    To set up:

    create table example ( id int, section varchar(25), subsection varchar(25) );
    
    insert into example select 0, 'dog', 'Labrador Retriever';
    insert into example select 1, 'deer', 'Whitetail';
    insert into example select 2, 'horse', 'Morgan';
    insert into example select 3, 'horse', 'Tarpan';
    insert into example select 4, 'deer', 'Row';
    insert into example select 5, 'horse', 'Appaloosa';
    insert into example select 6, 'dog', 'German Shephard';
    insert into example select 7, 'horse', 'Thoroughbred';
    insert into example select 8, 'dog', 'Mutt';
    insert into example select 9, 'horse', 'Welara Pony';
    insert into example select 10, 'dog', 'Cocker Spaniel';
    insert into example select 11, 'deer', 'American Elk/Wapiti';
    insert into example select 12, 'horse', 'Shetland Pony';
    insert into example select 13, 'deer', 'Chinese Water Deer';
    insert into example select 14, 'deer', 'Fallow';
    
    0 讨论(0)
  • 2020-11-22 05:23

    You can try this approach. This query returns 10 most populated cities for each country.

       SELECT city, country, population
       FROM
       (SELECT city, country, population, 
       @country_rank := IF(@current_country = country, @country_rank + 1, 1) AS country_rank,
       @current_country := country 
       FROM cities
       ORDER BY country, population DESC
       ) ranked
       WHERE country_rank <= 10;
    
    0 讨论(0)
  • 2020-11-22 05:25

    If you are using SQL 2005 you can do something like this...

    SELECT rs.Field1,rs.Field2 
        FROM (
            SELECT Field1,Field2, Rank() 
              over (Partition BY Section
                    ORDER BY RankCriteria DESC ) AS Rank
            FROM table
            ) rs WHERE Rank <= 10
    

    If your RankCriteria has ties then you may return more than 10 rows and Matt's solution may be better for you.

    0 讨论(0)
提交回复
热议问题