在Mysql中使用Union显示列底部的列和

时间:2012-09-02 05:03:28

标签: mysql

我遇到问题,我希望使用UNION显示列底部最后三列的总和,这是查询:

select a.app_file_id, a.app_fname,a.app_lname, l.tertiary_interest,l.loan_life,l.commit_date,
max(case  when date(r.receipt_date)<='2012-4-01' and r.receipt_date = (select max(r.receipt_date) from receipt_history r where date(r.receipt_date)<='2012-4-01' ) then amount_owed else 0 end),
sum(case when r.receipt_on='Principal' and r.receipt_date >= STR_TO_DATE(CONCAT(2012, '-04-01'),'%Y-%m-%d') and r.receipt_date <=  STR_TO_DATE(CONCAT(2012, '-03-31'), '%Y-%m-%d') + interval 1 year then r.receipt_amount end), 
max(case  when r.receipt_date<=STR_TO_DATE(concat(2013,'-4-01'),'%Y-%m-%d') and r.receipt_date = (select max(r.receipt_date) from receipt_history r where r.receipt_date<=STR_TO_DATE(concat(2013,'-4-01'),'%Y-%m-%d')) then amount_owed else 0 end) 
from applicant a 
left join loan l on l.l_app_file_id=a.app_file_id 
left join receipt_history r on r.r_app_file_id=a.app_file_id 
group by l.loan_id
union
select '', '','','','','',sum(max_pay_april_G),sum(fiscal_Prin_G),sum(final_bal )
from(
a.app_file_id, a.app_fname,a.app_lname, l.tertiary_interest,l.loan_life,l.commit_date,
max(case  when date(r.receipt_date)<='2012-4-01' and r.receipt_date = (select max(r.receipt_date) from receipt_history r where date(r.receipt_date)<='2012-4-01' ) then amount_owed else 0 end) as max_pay_april_G,
sum(case when r.receipt_on='Principal' and r.receipt_date >= STR_TO_DATE(CONCAT(2012, '-04-01'),'%Y-%m-%d') and r.receipt_date <=  STR_TO_DATE(CONCAT(2012, '-03-31'), '%Y-%m-%d') + interval 1 year then r.receipt_amount end) as fiscal_Prin_G, 
max(case  when r.receipt_date<=STR_TO_DATE(concat(2013,'-4-01'),'%Y-%m-%d') and r.receipt_date = (select max(r.receipt_date) from receipt_history r where r.receipt_date<=STR_TO_DATE(concat(2013,'-4-01'),'%Y-%m-%d')) then amount_owed else 0 end) as final_bal
from applicant a 
left join loan l on l.l_app_file_id=a.app_file_id 
left join receipt_history r on r.r_app_file_id=a.app_file_id 
group by l.loan_id
)q;

我知道这可能看起来令人困惑,因为我使用3个表并且列名可能会造成问题,但如果有人能帮助我分析查询并查看我出错的地方,我真的很感激帮助,到目前为止,我收到的错误消息是指示我到第13行,我希望我能够清楚地提出问题以获得一些帮助。非常感谢你

0 个答案:

没有答案