有sql脚本如下:use kdd99; create table kdd99_accounts ( account_id integer, district_id integer, frequency varchar(20), date DATE ); load data infile 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/kdd99_accounts.csv' into table kdd99_accounts CHARACTER SET gb2312 fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\r\n'; create table Kdd99_card( card_id integer, disp_id integer, issued DATE, type varchar(10) ); load data infile 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/Kdd99_card.csv' into table Kdd99_card CHARACTER SET gb2312 fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\r\n'; create table Kdd99_disp( disp_id integer, client_id integer, account_id integer, type varchar(6) ); load data infile 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/Kdd99_disp.csv' into table Kdd99_disp CHARACTER SET gb2312 fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\r\n'; create table Kdd99_trans( trans_id integer,account_id integer,date date,type varchar(2),operation varchar(20),amount long,balance long,k_symbol varchar(20),bank varchar(4),account long ); load data infile 'C:/ProgramData/MySQL/MySQL Server 8.0/Uploads/Kdd99_trans.csv' into table Kdd99_trans CHARACTER SET gb2312 fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by '\r\n';请问如何验证客户开通信用卡之后,其账户余额相较于开卡之前是否有提升
时间: 2024-01-06 10:06:29 浏览: 75
要验证客户开通信用卡之后,其账户余额相较于开卡之前是否有提升,可以根据以下步骤进行:
1. 对于每个客户,找到其开卡的日期(可以从 Kdd99_card 表中获取)和账户余额(可以从 Kdd99_accounts 表中获取)。
2. 找到客户所有的交易记录(可以从 Kdd99_trans 表中获取),筛选出交易类型为 "PRIJEM"(即存款)的记录,且操作描述为 "VKLAD"(即存入)的记录。这些记录表示客户向其账户中存入的金额。
3. 对于每个客户,计算其开卡后存入账户的总金额,即在开卡日期之后的所有符合条件的交易记录中,amount 字段的总和。
4. 将开卡前的账户余额和开卡后存入账户的总金额相加,得到客户开卡后的总账户余额。
5. 比较客户开卡后的总账户余额和开卡前的账户余额,如果前者大于后者,则说明客户开通信用卡后其账户余额有提升。
可以通过如下 SQL 语句实现以上步骤:
```
SELECT
kdd99_card.disp_id,
kdd99_card.issued AS card_issued_date,
kdd99_accounts.balance AS pre_card_balance,
SUM(CASE WHEN kdd99_trans.operation = 'VKLAD' AND kdd99_trans.type = 'PRIJEM'
THEN kdd99_trans.amount ELSE 0 END) AS post_card_deposit,
kdd99_accounts.balance + SUM(CASE WHEN kdd99_trans.operation = 'VKLAD' AND kdd99_trans.type = 'PRIJEM'
THEN kdd99_trans.amount ELSE 0 END) AS post_card_balance
FROM
kdd99_card
INNER JOIN kdd99_disp ON kdd99_card.disp_id = kdd99_disp.disp_id
INNER JOIN kdd99_accounts ON kdd99_disp.account_id = kdd99_accounts.account_id
LEFT JOIN kdd99_trans ON kdd99_accounts.account_id = kdd99_trans.account_id
AND kdd99_trans.date > kdd99_card.issued
GROUP BY
kdd99_card.disp_id, kdd99_card.issued, kdd99_accounts.balance
HAVING
post_card_balance > pre_card_balance;
```
该 SQL 语句将返回所有开通信用卡后账户余额有提升的客户的 disp_id(即客户表中的唯一标识)、信用卡开卡日期、开卡前账户余额、开卡后存入账户的总金额和开卡后总账户余额。
阅读全文