как совместить две колонки в оракуле - PullRequest
0 голосов
/ 07 мая 2018

у меня есть запрос

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?

image 1

image 2

Ответы [ 2 ]

0 голосов
/ 07 мая 2018

Попробуйте приведенный ниже 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
0 голосов
/ 07 мая 2018

Используйте функцию 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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...