因此,在这里我有一个案例来确定该用户每周是否迟到他的付款,以确定与付款日期和先前的过期字段的迟到比较
我有这样的样本数据
Name to be paid DATE PAID OVERDUE DATE
Bakrie 195000 2019-07-01 2019-07-08
Rocky 195000 2019-07-01 2019-07-08
Bakrie 195000 2019-07-15 2019-07-22
Bakrie 195000 2019-07-29 2019-08-05
Bakrie 195000 2019-08-05 2019-08-12
Febri 130000 2019-06-25 2019-07-02
希望生成的数据
Name to be paid DATE PAID OVERDUE DATE sign
Bakrie 195000 2019-07-01 2019-07-08 NOT LATE
Rocky 195000 2019-07-01 2019-07-08 NOT LATE
Bakrie 195000 2019-07-15 2019-07-22 LATE
Bakrie 195000 2019-07-29 2019-08-05 Late
Bakrie 195000 2019-08-05 2019-08-12 not late
Febri 130000 2019-06-25 2019-07-02 not late
您看到 用户名bakrie他延迟付款,因为在第二个付款日期中付款已超过了他的付款的先前记录到期日期中的到期日期
我已经尝试过这样的查询,但是失败了。他这样显示数据不晚
Bakrie 195000 2019-07-01 2019-07-08 NOT LATE
Bakrie 195000 2019-07-01 2019-07-08 NOT LATE
Bakrie 195000 2019-07-01 2019-07-08 NOT LATE
Bakrie 195000 2019-07-01 2019-07-08 NOT LATE
Rocky 195000 2019-07-01 2019-07-08 NOT LATE
Bakrie 195000 2019-07-15 2019-07-22 LATE
我的查询是这样的
select trackku.*, if(date paid > overdue_paid,'LATE','NOT LATE') from trackku
请帮助我!?!?!
答案 0 :(得分:0)
我根据您对问题的描述来到了以下查询。
select
a.name, a.paid, a.date_paid, a.overdue_date,
case
when max(b.overdue_date) is null or
max(b.overdue_date) >= a.date_paid then
'not late'
else
'late'
end as sign
from trackku as a
left join trackku as b
on a.name = b.name and a.date_paid > b.date_paid
group by a.name, a.paid, a.date_paid, a.overdue_date;
输出:
+--------+--------+------------+--------------+----------+
| name | paid | date_paid | overdue_date | sign |
+--------+--------+------------+--------------+----------+
| Bakrie | 195000 | 2019-07-01 | 2019-07-08 | not late |
| Bakrie | 195000 | 2019-07-15 | 2019-07-22 | late |
| Bakrie | 195000 | 2019-07-29 | 2019-08-05 | late |
| Bakrie | 195000 | 2019-08-05 | 2019-08-12 | not late |
| Febri | 130000 | 2019-06-25 | 2019-07-02 | not late |
| Rocky | 195000 | 2019-07-01 | 2019-07-08 | not late |
+--------+--------+------------+--------------+----------+
使用SQL Fiddle在线进行测试。