[英]How To determine this user late pay for this week in mysql
So here I have a case to determine this user he's late or not in his payments every week, to determine the late comparison to the date of payment and the previous overdue field所以在这里我有一个案例来确定这个用户他每周是否延迟付款,以确定与付款日期和上一个逾期字段的延迟比较
i have sample data like this我有这样的样本数据
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
data that wish to be generated希望生成的数据
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
as you see user name bakrie he late payment because in second payment date paid is passing due date in previous record due date of his payment正如你看到的用户名 bakrie 他延迟付款,因为在第二个付款日期支付的到期日超过了他付款的前一个记录到期日
I've tried querying like this but failed.我试过这样查询但失败了。 He shows data not late like this他这样显示数据不迟
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
my query like this`我的查询是这样的`
select trackku.*, if(date paid > overdue_paid,'LATE','NOT LATE') from trackku
please help me!?!?!请帮我!?!?!
I came to the following query based on your description of the problem.我根据您对问题的描述来到了以下查询。
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;
Output:输出:
+--------+--------+------------+--------------+----------+
| 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 |
+--------+--------+------------+--------------+----------+
Test it online with SQL Fiddle .使用SQL Fiddle在线测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.