Oto szczegółowy scenariusz:
Z podanymi danymi:
select a.title_id, sum(revenue), sum(cost),sum(interest) from
(select a.title_id, sum(revenue) as revenue, sum(cost) as cost from
(select 1 title_id, 10 revenue , 5 cost UNION all
select 2, 10, 5 UNION all
select 3, 10, 5 UNION all
select 4, 10, 5 UNION all
select 1, 20, 6 UNION all
select 2, 20, 6 UNION all
select 3, 20, 6 UNION all
select 4, 20, 6) as a
GROUP BY title_id) as a
left JOIN
(select title_id, sum(interest) as interest from
(select 1 as title_id, 10 as interest UNION all
select 2, 10 UNION all
select 3, 10 UNION all
select 4, 10 UNION all
select 1, 20 UNION all
select 2, 20 UNION all
select 3, 20 UNION all
select 4, 20) as b
GROUP BY title_id ) as b
on a.title_id = b.title_id
GROUP BY a.title_id
with ROLLUP
wynik:
1 30 11 30
2 30 11 30
3 30 11 30
4 30 11 30
120 44 120
ostateczna struktura zapytania:
select a.title_id, sum(revenue), sum(cost),sum(interest) from
(select a.title_id, sum(revenue) as revenue, sum(cost) as cost from
(select * from revenue) as a
GROUP BY title_id) as a
left JOIN
(select title_id, sum(interest) as interest from
(select * from fund) as b
GROUP BY title_id ) as b
on a.title_id = b.title_id
GROUP BY a.title_id
with ROLLUP