答案 0 :(得分:2)
转换查询以提供所需内容的最简单方法是:
SELECT
(
select SUM(Amount_income_table)
from bacci.income_table
where year(Date_income_table)='2017'
) AS `Actual Income`,
(
select SUM(estimated_amount)
from bacci.estimated_income_table
where estimated_year='2017'
) AS `Estimated Income`,
(
select SUM(Amount_expenses_table)
from bacci.expenses_table
where year(Date_expenses_table)='2017'
) AS `Actual Expenses`,
(
select SUM(estimated_amount)
from bacci.estimated_expenses_table
where estimated_year='2017'
) AS `Estimated Expenses`;
答案 1 :(得分:0)
您似乎想要:
select 'Total Income' as which,
(select sum(Amount_income_table)
from bacci.income_table
where year(Date_income_table) = 2017
) as actual,
(select sum(estimated_amount)
from bacci.estimated_income_table
where estimated_year = 2017
) as estimated
union all
select 'Total Expense' as which,
(select sum(Amount_expenses_table)
from bacci.expenses_table
where year(Date_expenses_table) = 2017
) as actual,
(select sum(estimated_amount)
from bacci.estimated_expenses_table
where estimated_year = 2017
) as estimated
然后,您可以获得总计,并获得额外的聚合级别:
select which, sum(actual) as actual, sum(estimated) as estimated
from ((select 'Total Income' as which,
(select sum(Amount_income_table)
from bacci.income_table
where year(Date_income_table) = 2017
) as actual,
(select sum(estimated_amount)
from bacci.estimated_income_table
where estimated_year = 2017
) as estimated
) union all
(select 'Total Expense' as which,
(select sum(Amount_expenses_table)
from bacci.expenses_table
where year(Date_expenses_table) = 2017
) as actual,
(select sum(estimated_amount)
from bacci.estimated_expenses_table
where estimated_year = 2017
) as estimated
)
) ae
group by which with rollup