虽然查询的输出看起来相当简单,但编写内联视图以生成可在外部 SELECT 中使用的值可能是一个优势,例如(使用您的示例表)
表格和数据
create table example ( id, day_ )
as
select 1, date '2020-02-02' from dual union all -- 2-2-20
select 2, date '2020-01-03' from dual union all -- 3-1-20
select 3, date '2020-02-15' from dual union all -- 15-2-20
select 4, date '2020-03-20' from dual -- 20-3-20
;
第一步(将其用于“内联视图”)
select
to_char( day_, 'MM-YY') month_
, to_char( extract ( day from last_day( day_ ) ) ) lastday_
, count(*) over ( order by to_char( day_, 'MM-YY') ) runningtotal_
, row_number() over ( partition by to_char( day_, 'MM-YY') order by day_ ) rn_
from example ;
-- result
+------+--------+-------------+---+
|MONTH_|LASTDAY_|RUNNINGTOTAL_|RN_|
+------+--------+-------------+---+
|01-20 |31 |1 |1 |
|02-20 |29 |3 |1 |
|02-20 |29 |3 |2 |
|03-20 |31 |4 |1 |
+------+--------+-------------+---+
最终查询
select
month_
, runningtotal_ rows_num
, round( max( rn_ ) / lastday_, 5 ) avg_per_day
, to_char( max( rn_ ) ) || '/' || to_char( lastday_ ) avg_per_day
from (
select
to_char( day_, 'MM-YY') month_
, to_char( extract ( day from last_day( day_ ) ) ) lastday_
, count(*) over ( order by to_char( day_, 'MM-YY') ) runningtotal_
, row_number() over ( partition by to_char( day_, 'MM-YY') order by day_ ) rn_
from example
)
group by month_, runningtotal_, lastday_
order by month_
;
-- result
+------+--------+-----------+-----------+
|MONTH_|ROWS_NUM|AVG_PER_DAY|AVG_PER_DAY|
+------+--------+-----------+-----------+
|01-20 |1 |0.03226 |1/31 |
|02-20 |3 |0.06897 |2/29 |
|03-20 |4 |0.03226 |1/31 |
+------+--------+-----------+-----------+
DBfiddle here.
注意:(对我而言) 您需要哪种形式的“AVG_PER_DAY”并不太清楚。只需从外部 SELECT 中删除不需要的行即可。