谷歌大查询

时间:2019-02-27 19:07:51

标签: mysql google-bigquery

假设有一个包含两个表的数据库,分别是“贷款”和“帐单支付”。

对于“贷款”表,我具有以下列:

loanId,
clientId(primary key),
DisbursementDate,
applicationDate,
approvalDate,
loanNumber,
loanAmount,
interestRate

Billpayment表列包括:

billId(primary),
customerId(foreign),
billAmount,
payment_status[success/fail],
billDate

如何获取在2018年3月支付账单的客户,并显示在2018年剩余月份中至少支付账单的客户数量,此外,还要显示是否账单付款客户是否在2018年内有贷款?

1 个答案:

答案 0 :(得分:1)

好的,让我们开始吧。我没有测试过任何一个,也没有对其进行适当的优化,但是希望它会有所帮助。

要列出2018年3月的付款,假设我们不在乎是否成功,请执行以下操作:

select * from billpayment
where month(billDate) = 3 and year(billDate) = 2018

要在上面包含相关的客户信息,请加入:

select * from billpayment
join loans on customerId = clientId
where month(billDate) = 3 and year(billDate) = 2018

要仅列出在2018年的另一个月内也进行过付款的客户,请再次加入派生表:

select * from billpayment b
join loans l on b.customerId = l.clientId
join (select distinct b1.clientId as clientid1 from billpayment b1
      where year(b1.billdate) = 2018 and month(b1.billdate) <> 3) c
        on b.customerId = c.clientId1 
where month(b.billDate) = 3 and year(b.billDate) = 2018

要检查他们是否在2018年开始贷款(假设申请日期为准),请在您的where子句中添加and:

select * from billpayment b
join loans l on b.customerId = l.clientId
join (select distinct b1.clientId as clientid1 from billpayment b1
      where year(b1.billdate) = 2018 and month(b1.billdate) <> 3) c
        on b.customerId = c.clientId1 
where month(b.billDate) = 3 and year(b.billDate) = 2018
and year(l.applicationDate) = 2018