How to select the sencond,third record of each category?

user2231793

I want get the 2nd and 3nd largest of each category? How to do this ? Sorry my bad English

I have table tin_tuc like this

||id || id_cat ||some information
||1  || 1      ||      
||2  || 1      ||
||3  || 1      ||
||4  || 1      ||
||5  || 2      ||
||6  || 2      || 
||7  || 3      ||
||8  || 3      ||
||9  || 3      ||

Now i want to get two record each category that have value large 2nd and 3th(top 3 except largest value).
the out put i want

||id || id_cat ||some information
||2  || 1      ||
||3  || 1      ||
||5  || 2      ||
||6  || 2      || 
||8  || 3      ||
||9  || 3      ||
KKK

The following query will return top 2nd and 3rd song based on category from a table:

set @num := 0, @category := '';

select
    songID, `categoryId`, hits
    from 
        (
        select songID, categoryid, hits,
              @num := if(@category = `categoryid`, @num + 1, 1) as row_number,
              @category := `categoryid` as dummy
          from songs
          where categoryid is not null
          order by `categoryid`, hits desc
        ) as temptable

    where temptable.row_number=3 or temptable.row_number=2;

here i am sorting all records by category and hits(my priority for example you can select your highest point holding field) in sub-query and then giving each record an incrementing number starting from 1 and resetting when category changes then finally i select 2nd and 3rd record in outer query.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to keep earliest record for each category but without considering the extra columns?

From Dev

How to Select record between a given time range and Item Category

From Dev

How to select the last record of each ID

From Dev

How to select greatest record (semester) of each id

From Dev

How do I select an exact number of articles for each category?

From Dev

How to select all categories and count number of each category articles in MySQL

From Dev

How to select all categories and count number of each category articles in MySQL

From Dev

How To SELECT Category For Each Post Id In PHP Function

From Dev

iSeries: SQLRPGLE How to call a procedure for each record in a SELECT statement

From Dev

how to select last 30 days mysql record and group by each day

From Dev

How to select 200 record for each 'for loop' iteration in oracle?

From Dev

How do I select the latest record for each disticnt id in Hive?

From Dev

How to select just one record for each particular value in SQL?

From Dev

Select last record of each person

From Dev

Select the highest record of each relation

From Dev

select top 2 rows in each category

From Dev

SQL Select a single category for each distinct id

From Dev

MySQL Select top 10 items for each category

From Dev

Postgresql : How do I select top n percent(%) entries from each group/category

From Dev

How can I get 5 records for each 'category' with one select in MySQL?

From Dev

How To Select One Top Of Each Record By Id in Second Joined Table In SQL Server SELECT?

From Dev

Select Highest value against each record in SQL

From Dev

Select max record of each group on a group by

From Dev

Select only most recent record for each month

From Dev

LINQ select next record with each matching result

From Dev

Group by ID, select only the newest record for each

From Dev

Select only most recent record for each month

From Dev

Select max record of each group on a group by

From Dev

Table has one record per employee per year. How do I select the record for each employee for the latest year for that employee?

Related Related

  1. 1

    How to keep earliest record for each category but without considering the extra columns?

  2. 2

    How to Select record between a given time range and Item Category

  3. 3

    How to select the last record of each ID

  4. 4

    How to select greatest record (semester) of each id

  5. 5

    How do I select an exact number of articles for each category?

  6. 6

    How to select all categories and count number of each category articles in MySQL

  7. 7

    How to select all categories and count number of each category articles in MySQL

  8. 8

    How To SELECT Category For Each Post Id In PHP Function

  9. 9

    iSeries: SQLRPGLE How to call a procedure for each record in a SELECT statement

  10. 10

    how to select last 30 days mysql record and group by each day

  11. 11

    How to select 200 record for each 'for loop' iteration in oracle?

  12. 12

    How do I select the latest record for each disticnt id in Hive?

  13. 13

    How to select just one record for each particular value in SQL?

  14. 14

    Select last record of each person

  15. 15

    Select the highest record of each relation

  16. 16

    select top 2 rows in each category

  17. 17

    SQL Select a single category for each distinct id

  18. 18

    MySQL Select top 10 items for each category

  19. 19

    Postgresql : How do I select top n percent(%) entries from each group/category

  20. 20

    How can I get 5 records for each 'category' with one select in MySQL?

  21. 21

    How To Select One Top Of Each Record By Id in Second Joined Table In SQL Server SELECT?

  22. 22

    Select Highest value against each record in SQL

  23. 23

    Select max record of each group on a group by

  24. 24

    Select only most recent record for each month

  25. 25

    LINQ select next record with each matching result

  26. 26

    Group by ID, select only the newest record for each

  27. 27

    Select only most recent record for each month

  28. 28

    Select max record of each group on a group by

  29. 29

    Table has one record per employee per year. How do I select the record for each employee for the latest year for that employee?

HotTag

Archive