我有一个查询
select
city,
month,
month_number,
sum(totalcount) as totalcount,
sum(total_value) total_value
from
(
select
city,
to_char( testdate, 'Mon') month,
to_number( to_char( testdate, 'mm' ) ) month_number,
count( totalcount ) totalcount,
sum( total_value ) total_value
from
testtable
group by
city,
testdate
)
group by
city,
month,
month_number
order by
1,3
将结果显示为图像1,如何修改此查询可以将结果显示为图像2?
答案 0 :(得分:1)
在group by中使用Oracle ROLLUP函数来实现所需的结果。
select
city,
month,
month_number,
sum(totalcount) as totalcount,
sum(total_value) total_value
from
(
select
city,
to_char( testdate, 'Mon') month,
to_number( to_char( testdate, 'mm' ) ) month_number,
count( totalcount ) totalcount,
sum( total_value ) total_value
from
testtable
group by
city,
testdate
)
group by
city,
ROLLUP (month,month_number)
order by
1,3
答案 1 :(得分:0)
尝试以下SQL。
SELECT city, month,month_number
sum(totalcount) as totalcount,
sum(total_value) total_value
FROM ( select
city,
to_char( testdate, 'Mon') month,
to_number( to_char( testdate, 'mm' ) ) month_number,
count( totalcount ) totalcount,
sum( total_value ) total_value
from
testtable
group by
city,
testdate) x
GROUP BY ROLLUP (city, month,month_number)
ORDER BY 1,3