下面会得到结果的查询是什么(见图)?
期末余额公式:存款+期初余额 - 交易 第二天的期初余额应为前一天的期末余额
create database sample_dbbal;
create table beginning_balances
(
postdate date, person varchar(30), currency varchar(10), beginning_balance int
) ;
insert into beginning_balance ( postdate, person, currency,beginning_balance)
values ('1/25/2015', 'johnpaul','usd','500')
;
create table transaction_details
(postdate date, person varchar(30), currency varchar(10), transactionamount int
) ;
insert into transaction_details (postdate, person, currency, transactionamount)
values ('1/25/2015', 'johnpaul', 'usd', '55');
insert into transaction_details (postdate, person, currency, transactionamount)
values ('1/28/2015', 'johnpaul', 'usd', '68');
insert into transaction_details (postdate, person, currency, transactionamount)
values ('1/30/2015', 'johnpaul','usd', '20');
insert into transaction_details (postdate, person, currency, transactionamount)
values ('2/20/2015', 'johnpaul', 'usd', '10');
create table deposit_details
(postdate date, person varchar(30), currency varchar(10), deposit int
) ;
insert into deposit_details (postdate, person,currency,deposit)
values ('1/25/2015', 'johnpaul', 'usd', '50');
insert into deposit_details (postdate, person,currency,deposit)
values ('1/28/2015', 'johnpaul', 'usd', '300');
insert into deposit_details (postdate, person,currency,deposit)
values ('1/28/2015', 'johnpaul', 'usd', '25');
insert into deposit_details (postdate, person,currency,deposit)
values ('1/25/2015', 'johnpaul', 'usd', '100');
insert into deposit_details (postdate, person,currency,deposit)
values ('1/25/2015', 'johnpaul', 'usd', '20');
select
`postdate`,
`person`,
`currency`,
sum(`totaldeposit`),
sum( `totalamount`),
sum( `beginning_balance`),
(sum(`totaldeposit`) + sum( `beginning_balance`)) - sum( `totalamount`) as `endingbalance`
from
(
select `postdate`,
`person`,
`currency`,
sum(deposit) as `totaldeposit`,
0 as `totalamount`,
0 as `beginning_balance`
from deposit_details
group by postdate,person,currency
union all
select `postdate`,
`person`,
`currency`,
0 as `totaldeposit`,
sum(transactionamount) as `totalamount`,
0 as `beginning_balance`
from transaction_details
group by postdate,person,currency
union all
select `postdate`,
`person`,
`currency`,
0 as `totaldeposit`,
0 as `totalamount`,
`beginning_balance`
from beginning_balances
group by postdate,person,currency
) temp123
group by `postdate`,`person`,`currency`
- 我不知道如何继续计算Ending Balance。