简体   繁体   中英

How to get MAX/MIN function for each month in my data set?

I'm needing to provide usage data and need to summarize it by month.

In my code I have it only looking at JAN of 2018 so find the Max and Min based on the current data set is pretty straight forward. If I was to expand it for all of 2018 I'd currently show the MAX and MIN for the entire year.

      SELECT DISTINCT
         bi_vwn_co_acct,
         bi_consumer_view_1.bi_format_name,
         bi_srv_loc.bi_city,
         bi_srv_loc.bi_st,
         bi_srv_loc.bi_x_coord,
         bi_srv_loc.bi_y_coord,
         bi_interval_rdgs.bi_mtr_nbr,
         MAX (bi_interval_rdgs.bi_rdg),
         MIN (bi_interval_rdgs.bi_rdg),
         (MAX (bi_interval_rdgs.bi_rdg) - MIN (bi_interval_rdgs.bi_rdg))
            AS monthusage
    FROM bi_srv_loc
         INNER JOIN bi_srv_link
            ON bi_srv_loc.bi_srv_loc_nbr = bi_srv_link.bi_srv_loc_nbr
         INNER JOIN bi_type_service
            ON bi_srv_link.bi_srv_loc_nbr = bi_type_service.bi_srv_loc_nbr
         INNER JOIN bi_consumer_view_1
            ON bi_type_service.bi_acct = bi_consumer_view_1.bi_vwn_co_acct
         INNER JOIN bi_interval_rdgs
            ON bi_srv_link.bi_mtr_nbr = bi_interval_rdgs.bi_mtr_nbr
   WHERE     bi_interval_rdg_dt_tm >= '01-JAN-2018'
         AND bi_interval_rdg_dt_tm <= '31-JAN-2018'
         AND bi_type_service.bi_srv_stat_cd IN ('1',
                                                '7',
                                                '18',
                                                '21',
                                                '22',
                                                '30')
GROUP BY bi_consumer_view_1.bi_vwn_co_acct,
         bi_consumer_view_1.bi_format_name,
         bi_srv_loc.bi_x_coord,
         bi_srv_loc.bi_y_coord,
         bi_interval_rdgs.bi_mtr_nbr,
         bi_srv_loc.bi_city,
         bi_srv_loc.bi_st

I'm hoping to get a list of data that looks something like this:

ACCT1 JAN MONTHUSAGE: 10
ACCT1 FEB MONTHUSAGE: 13
ACCT1 MAR MONTHUSAGE: 12
...
ACCT2 JAN MONTHUSAGE: 23
ACCT2 FEB MONTHUSAGE: 18

There's obviously more fields shown in my code but in general this is what I'm after. Somehow I need to be able to go through a given date range and section it off by month. Any help or guidance would be greatly appreciated!

将查询调整为GROUP BY MONTH(BI_INTERVAL_RDG_DT_TM), YEAR(BI_INTERVAL_RDG_DT_TM )

I'm not sure I understood which column (out of all those) represents data value, so - here's an example based on Scott's sample schema which might give you idea - extract month and year information out of the date column (for example, by using TO_CHAR function) and include it into the GROUP BY clause.

SQL> select to_char(e.hiredate, 'yyyymm') mon,
  2         min(e.sal) minsal,
  3         max(e.sal) maxsal
  4  from emp e
  5  group by to_char(e.hiredate, 'yyyymm')
  6  order by to_char(e.hiredate, 'yyyymm');

MON        MINSAL     MAXSAL
------ ---------- ----------
198012        800        800
198102       1250       1600
198104       2975       2975
198105       2850       2850
198106       2450       2450
198109       1250       1500
198111       5000       5000
198112        950       3000
198201       1300       1300
198212       3000       3000
198301       1100       1100

11 rows selected.

SQL>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM