使用MAX DATE和GROUP BY

时间:2015-12-15 03:39:02

标签: mysql database greatest-n-per-group

我有一个表名payment_schedule,其中包含以下内容

payment_schedule

我想用MAX(due_date) GROUPED BY loan_application_id

获取记录

参考上面图片中的记录,我希望结果如下:

enter image description here

我尝试使用以下SQL查询

SELECT
    id,
    MAX(due_date) as due_date,
    loan_application_id
FROM
    payment_schedule
GROUP BY
    loan_application_id

返回以下结果。

enter image description here

如您所见,对于给定的截止日期,它不会返回相应的id

此外,我还有另一个名为payment_type_id的列,我需要在payment_type_id的值为3时排除行。

我在这里尝试了几种解决方案,似乎没什么用,怎么去呢?

感谢。

3 个答案:

答案 0 :(得分:3)

这称为 Group-wise Maximum ,此处标记为。最传统的方法是找到您想要的值并进行连接以获得每组的相应行,如下所示:

SELECT
    ps.id,
    ps.due_date,
    ps.loan_application_id
FROM
(
    SELECT
        MAX(due_date) as due_date,
        loan_application_id
    FROM payment_schedule
    WHERE payment_type_id != '3'
    GROUP BY loan_application_id
) ps2
    LEFT JOIN payment_schedule ps USING (loan_application_id)
WHERE ps.due_date = ps2.due_date
    AND ps.payment_type_id != '3'
GROUP BY ps.loan_application_id

还值得一提的是,如果您的loan_application_iddue_date列上有索引,此查询的运行速度将提高数十倍。

我在SO上看到的最佳讨论是:Select first row in each GROUP BY group?

此处的官方文档中也提到了http://dev.mysql.com/doc/refman/5.7/en/example-maximum-column-group-row.html

答案 1 :(得分:2)

如果loan_application_id的截止日期不同,您可以在下方删除关键字distinct:

select distinct a.*
from payment_schedule a, (
    select loan_application_id, max(due_date) max_date
    from payment_schedule
    where payment_type_id <> 3
    group by 1
) as b
where a.loan_application_id = b.loan_application_id
and a.due_date = b.max_date

答案 2 :(得分:0)

在大多数数据库中,这最容易使用窗口函数。在MySQL中,您可以使用joingroup by

select ps.*
from payment_schedule ps join
     (select load_application_id, max(due_date) as maxdd
      from payment_schedule
      group by load_application_id
     ) l
     on ps.load_application_id = l.load_application_id and ps.due_date = l.maxdd;