0

我有一个问题,我想使用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 行,我希望我把问题说得足够清楚以获得一些帮助。非常感谢你

4

0 回答 0