SQL查询在Oracle中按月分组数据

时间:2018-03-30 10:01:25

标签: sql oracle select

我的表格输出格式如下。

从CAPACITY_MON.TABLESPACE_GROWTH_HIST中选择*,其中instance ='MONDAY';

INSTANCE         TABLESPACE_NAME                TOTAL_SIZE TOTAL_USED TOTAL_FREE   MAX_FREE REPORT_DA
---------------- ------------------------------ ---------- ---------- ---------- ---------- ---------
MONDAY           MONDAY_IDX                        3145728    2914624     231104     230400 27-MAR-18
MONDAY           SYSAUX                           14848000   14138112     709888     706560 27-MAR-18
MONDAY           SYSTEM                            2314240    2261120      53120      51200 27-MAR-18
MONDAY           MONDAY_DATA                        716800     456832     259968     259072 27-MAR-18
MONDAY           USERS                                5120       1984       3136       3136 27-MAR-18
MONDAY           MONDAY_DATA                       5242880    4236352    1006528    1005568 27-MAR-18
MONDAY           MONDAY_IDX                        3145728    2914624     231104     230400 28-MAR-18
MONDAY           SYSAUX                           14848000   14138688     709312     706560 28-MAR-18
MONDAY           SYSTEM                            2314240    2261120      53120      51200 28-MAR-18
MONDAY           MONDAY_DATA                        716800     456832     259968     259072 28-MAR-18
MONDAY           USERS                                5120       1984       3136       3136 28-MAR-18
MONDAY           MONDAY_DATA                       5242880    4236352    1006528    1005568 28-MAR-18
MONDAY           MONDAY_IDX                        3145728    2914624     231104     230400 29-MAR-18
MONDAY           SYSAUX                           14848000   14136896     711104     706560 29-MAR-18
MONDAY           SYSTEM                            2314240    2261120      53120      51200 29-MAR-18
MONDAY           MONDAY_DATA                        716800     456832     259968     259072 29-MAR-18
MONDAY           USERS                                5120       1984       3136       3136 29-MAR-18
MONDAY           MONDAY_DATA                       5242880    4236352    1006528    1005568 29-MAR-18
MONDAY           MONDAY_IDX                        3145728    2914624     231104     230400 30-MAR-18
MONDAY           SYSAUX                           14848000   14137152     710848     706560 30-MAR-18
MONDAY           SYSTEM                            2314240    2261120      53120      51200 30-MAR-18
MONDAY           MONDAY_DATA                        716800     456832     259968     259072 30-MAR-18
MONDAY           USERS                                5120       1984       3136       3136 30-MAR-18
MONDAY           MONDAY_DATA                       5242880    4236352    1006528    1005568 30-MAR-18

如何以下列格式获得上述输出

我需要每月获得每个表空间的最大值(TOTAL_USED)。列应基于下面提到的月份。

TABLESPACE_NAME Mar-2018      Apr-2018
MONDAY_DATA         4236352       32655
MONDAY_IDX          2914624       212334
SYSTEM          2261120       23244
SYSAUX          14138688      123344
USERS           1984          3235666

我试过这个查询,

select TABLESPACE_NAME,max(TOTAL_USED),round(report_DATE,'month') from CAPACITY_MON.TABLESPACE_GROWTH_HIST where instance='MONDAY' group by TABLESPACE_NAME,report_date order by report_date;

它没有按预期提供输出。

如何实现上述输出!

2 个答案:

答案 0 :(得分:1)

您希望使用TRUNC()代替ROUND()并将其分组,而不是使用REPORT_DATE:

select TABLESPACE_NAME, max(TOTAL_USED), TRUNC(report_DATE, 'month') AS report_month
  from CAPACITY_MON.TABLESPACE_GROWTH_HIST 
 where instance = 'MOSA801M'
 group by TABLESPACE_NAME, TRUNC(report_date, 'MONTH');

此外,您在问题中指定的格式看起来像是您想要转动的数据。这是一项更多的工作,但并不困难:

SELECT tablespace_name
     , MAX(CASE WHEN report_month = DATE'2018-03-01' THEN max_total_used END) AS "Mar-2018"
     , MAX(CASE WHEN report_month = DATE'2018-04-01' THEN max_total_used END) AS "Apr-2018"
  FROM (
    select TABLESPACE_NAME, max(TOTAL_USED) AS max_total_used, TRUNC(report_DATE, 'month') AS report_month
      from CAPACITY_MON.TABLESPACE_GROWTH_HIST 
     where instance = 'MOSA801M'
     group by TABLESPACE_NAME, TRUNC(report_date, 'MONTH')
) GROUP BY tablespace_name
 ORDER BY tablespace_name;

希望这有帮助。

编辑:我认为您可能会“动态”地导出列(但不是它们的标题),如下所示:

SELECT tablespace_name
     ,MAX(CASE WHEN report_month = TRUNC(ADD_MONTHS(SYSDATE,-1),'MONTH') THEN max_total_used END) AS "Last Month"
     ,MAX(CASE WHEN report_month = TRUNC(SYSDATE,'MONTH') THEN max_total_used END) AS "This Month"
  FROM (
    select TABLESPACE_NAME, max(TOTAL_USED) AS max_total_used, TRUNC(report_DATE, 'month') AS report_month
      from CAPACITY_MON.TABLESPACE_GROWTH_HIST 
     where instance = 'MOSA801M'
     group by TABLESPACE_NAME, TRUNC(report_date, 'MONTH')
) GROUP BY tablespace_name
 ORDER BY tablespace_name;

P.S。您可以使用PL / SQL和动态SQL获取动态列标题。但我认为这不值得努力。

答案 1 :(得分:0)

使用TO_CHAR()从日期获取月份

select TABLESPACE_NAME,
       max(TOTAL_USED) as 'Mar-2018',
       to_char(report_date, 'Month') AS report_Month
from CAPACITY_MON.TABLESPACE_GROWTH_HIST 
where instance='MOSA801M' and to_char(report_date, 'YYYY')='2018'
group by TABLESPACE_NAME,to_char(report_date, 'Month');