mysql - Conditional SQL Query on Multiple Rows -


i trying find out customers have defaulted on loans. query dataset find user_id of customers have not paid in last 60 days, , not sure how implement in sql.

user_id    due_date                loan_amount    paid_amount 1          2012-04-04 16:14:12    500            40 1          2012-05-04 16:14:12    500            40 1          2012-06-04 16:14:12    500            0 1          2012-07-04 16:14:12    500            0 1          2012-08-04 16:14:12    500            0 2          2012-02-15 03:30:55    2030           100 2          2012-03-15 03:30:55    2030           100 2          2012-04-15 03:30:55    2030           100 3          2012-01-03 12:24:42    777            10 3          2012-02-03 12:24:42    777            0 3          2012-03-03 12:24:42    777            0 3          2012-04-03 12:24:42    777            0 

in pseudocode (shown in bold) this, can't seem implement in mysql:

select user_id table_name loan_amount > 0 , [the value paid_amount has been null on 60 days]

desired output: users 1 , 3 in above query returned because have not paid 3 consecutive periods.

note: due_date time stamp

any ideas appreciated!

looks can use datediff(date1, date1) function obtain list of delinquent borrowers.

select distinct      user_id table_name n      join (select user_id, max(due_date) maxdate table_name group user_id) t            on n.user_id = t.user_id           , n.due_date = t.maxdate       loan_amount > 0      , paid_amount null      amd datediff(due_date, getdate()) > 60   

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -