What is the best way to select the first two records of each group by a “SELECT” command?

后端 未结 3 722
别跟我提以往
别跟我提以往 2021-02-10 05:30

For instance I have the following table:

id group data
1 1 aaa
2 1 aaa
3 2 aaa
4 2 aaa
5 2 aaa
6 3 aaa
7 3 aaa
8 3 aaa

What is the best way to

3条回答
  •  我寻月下人不归
    2021-02-10 05:49

    You select, filter and order your query like normal and then

    for MSSQL

    SELECT TOP 2 * FROM foo; 
    

    From what I can remember Sybase, Oracle and possible a few other RDBMS's uses this syntax to.

    for MySQL you do

    SELECT * FROM foo LIMIT 2; 
    

    Update:

    Yes, I misread your question, sorry. Seems like a few of us did :)

    Then it depends on whether you RDBMS supports HAVING or not etc. You could construct a query using HAVING or using IN and a subquery in the IN clause.

    For MSSQL I think you could do something like (code not tested)

    SELECT id, data
        FROM (
            SELECT id, data, Rank() over (Partition BY group ORDER BY id DESC ) AS Rank
            FROM table
            ) rs WHERE Rank <= 2)
    

    But since this depends on your RDBMS I ask you to look at similar questions and see which one works best for your case since MSSQL supports some things MySQL doesn't and the other way around.

    Here are some examples

    Select top 10 records for each category

    How to select the last two records for each topic_id in MySQL

提交回复
热议问题