1st Row in Group By vs. Last Row

后端 未结 5 775
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-21 10:23

Wondering how to return the 1st row in a MySQL group by vs. the last row. Returning a series of dates, but need the first one displayed.

select * from calendar w         


        
5条回答
  •  一向
    一向 (楼主)
    2021-01-21 11:10

    Use the min function insead of max to return the first row in a group vs the last row.

    select min(date) from calendar ...
    

    If you want the entire row for each group, join the table with itself filtered for the min dates on each group:

    select c2.* from
        (select eventid, min(dt) dt from calendar
            where approved = 'pending' group by eventid) c1
    inner join calendar c2 on c1.eventid=c2.eventid and c1.dt=c2.dt
    group by eventid;
    

    Demo: http://www.sqlize.com/6lOr55p67c

提交回复
热议问题