2066. 账户余额

SQL架构

表名: Transactions

+-------------+------+
| Column Name | Type |
+-------------+------+
| account_id  | int  |
| day         | date |
| type        | ENUM |
| amount      | int  |
+-------------+------+
(account_id, day) 是该Transactions表的主键.
表中的每行数据表示一次交易的信息, 包括此次交易的账号(account_id), 交易类型(type), 交易发生时间(day), 交易发生金额(amount).
其中交易类型(type)字段包括了两种行为:存入 ('Deposit'), 取出('Withdraw').

请写出能够返回用户每次交易完成后的账户余额. 我们约定所有用户在进行交易前的账户余额都为0, 并且保证所有交易行为后的余额不为负数。

返回的结果请依次按照 账户(account_id), 日期( day ) 进行升序排序 .

查询结果的格式请参照以下测试样例.

测试样例1:

输入: 
Transactions 表:
+------------+------------+----------+--------+
| account_id | day        | type     | amount |
+------------+------------+----------+--------+
| 1          | 2021-11-07 | Deposit  | 2000   |
| 1          | 2021-11-09 | Withdraw | 1000   |
| 1          | 2021-11-11 | Deposit  | 3000   |
| 2          | 2021-12-07 | Deposit  | 7000   |
| 2          | 2021-12-12 | Withdraw | 7000   |
+------------+------------+----------+--------+
输出: 
+------------+------------+---------+
| account_id | day        | balance |
+------------+------------+---------+
| 1          | 2021-11-07 | 2000    |
| 1          | 2021-11-09 | 1000    |
| 1          | 2021-11-11 | 4000    |
| 2          | 2021-12-07 | 7000    |
| 2          | 2021-12-12 | 0       |
+------------+------------+---------+

解释: 
账户1:
- 初始金额为 0.
- 2021-11-07 --> 存入2000. 余额变为 0 + 2000 = 2000.
- 2021-11-09 --> 取出1000. 余额变为 2000 - 1000 = 1000.
- 2021-11-11 --> 存入3000. 余额变为 1000 + 3000 = 4000.
账户2:
- 初始金额为 0.
- 2021-12-07 --> 存入7000. 余额变为 0 + 7000 = 7000.
- 2021-12-12 --> 取出 7000. 余额变为 7000 - 7000 = 0.
select
account_id ,`day`,sum(if(type ="Deposit",amount,-amount)) over(partition by account_id order by `day` rows between unbounded preceding and current row)  balance
from
Transactions
order by account_id,`day`

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很抱歉,我无法直接为您提供代码,但我可以为您提供一些基本的思路和代码片段,以供参考。 首先,我们需要创建一个基类 Account,包含账户号码、账户类型、账户名、开户日期、账户余额等属性和方法: ```c++ class Account { protected: int accountNumber; string accountType; string accountName; string openDate; double balance; public: Account(int accountNumber, string accountType, string accountName, string openDate, double balance); virtual ~Account(); virtual void deposit(double amount); virtual void withdraw(double amount); virtual void transfer(Account* dest, double amount); virtual double getBalance(); virtual void showInfo(); virtual void modifyAccount(string accountType, string accountName); virtual void closeAccount(); }; ``` 接着,我们需要创建两个子类 SavingAccount 和 CheckingAccount,分别继承自 Account 类: ```c++ class SavingAccount : public Account { public: SavingAccount(int accountNumber, string accountName, string openDate, double balance); ~SavingAccount(); }; class CheckingAccount : public Account { public: CheckingAccount(int accountNumber, string accountName, string openDate, double balance); ~CheckingAccount(); }; ``` 在子类中,我们可以根据需要重写父类的虚函数,实现多态性。 例如,在 SavingAccount 中,我们可以重写 deposit 函数,实现存款的操作: ```c++ void SavingAccount::deposit(double amount) { balance += amount; cout << "成功存款 " << amount << " 元,当余额为 " << balance << " 元。" << endl; } ``` 接着,我们可以创建一个 AccountManager 类,用于管理所有的账户信息: ```c++ class AccountManager { private: vector<Account*> accounts; public: AccountManager(); ~AccountManager(); void addAccount(Account* account); void removeAccount(int accountNumber); Account* findAccountByNumber(int accountNumber); void deposit(int accountNumber, double amount); void withdraw(int accountNumber, double amount); void transfer(int srcAccountNumber, int destAccountNumber, double amount); void showAccountInfo(int accountNumber); void modifyAccountInfo(int accountNumber, string accountType, string accountName); void closeAccount(int accountNumber); }; ``` 在 AccountManager 中,我们可以添加账户、删除账户、查找账户进行存款、取款、转等操作。同时,我们也可以提供良好的用户界面,优化用户的体验感。 最后,我们可以在 main 函数中创建一个 AccountManager 实例,并测试各种功能: ```c++ int main() { AccountManager manager; // 添加账户 Account* account1 = new SavingAccount(1001, "张三", "2020-01-01", 1000); manager.addAccount(account1); Account* account2 = new CheckingAccount(1002, "李四", "2020-01-01", 1000); manager.addAccount(account2); // 存款 manager.deposit(1001, 500); // 取款 manager.withdraw(1002, 200); // 转 manager.transfer(1001, 1002, 300); // 查询余额 manager.showAccountInfo(1001); manager.showAccountInfo(1002); // 修改信息 manager.modifyAccountInfo(1001, "储蓄账户", "王五"); // 注销账户 manager.closeAccount(1002); return 0; } ``` 这只是一个简单的示例,您可以进一步扩展并完善系统的功能。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值