在Mysql中使用Union在列底部显示列和
我遇到了一个问题,我想使用在Mysql中使用Union在列底部显示列和,mysql,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_his
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;
选择a.app\u文件\u id、a.app\u fname、a.app\u lname、l.treative\u interest、l.loan\u life、l.commit\u date、,
最大值(接收日期(r.接收日期)几件事。在没有GROUP BY
语句的情况下使用聚合函数是一种糟糕的做法。是否确实要使用UNION
而不是UNION ALL
?我现在将坚持使用UNION,我只是无法发现问题所在。请先分解查询。在取消之前和之后运行查询ION
…尝试了这一点,但仍然得到相同的错误您考虑过使用ROLLUP吗?