mysql group by having min

痞子三分冷 提交于 2019-12-03 09:08:23

You can do this the following way:

select t1.id_ctr,
    t1.account_number_cus,
    t1.original_date_ctr,
    t1.mrc_ctr  
from yourtable t1
inner join
(
    select min(original_date_ctr) as mindate, account_number_cus
    from yourtable
    group by account_number_cus
) t2
    on t1.account_number_cus = t2.account_number_cus
    and t1.original_date_ctr = t2.mindate

see SQL Fiddle with Demo

Doing this with a join will be faster:

SELECT a.*
FROM mytable a
LEFT JOIN mytable b
  ON a.account_number_cus = b.account_number_cus
  AND b.original_date_ctr < a.original_date_ctr
WHERE b.id_ctr IS NULL
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!