SELECT bb.jidu,sum(score) as num,bb.allnum,trunc(((sum(score)/allnum)),2) ratio
from
(
SELECT '第一季度' as jidu ,
CASE WHEN to_date('2021-03-01','yyyy-MM-dd') > t.bytime and t.bytime >= to_date('2021-01-01','yyyy-MM-dd') then 1 else 0 end as score,
(select COUNT(1) from BUYTICKETS) AS ALLNUM
FROM BUYTICKETS t
UNION ALL
SELECT '第二季度' as jidu ,
CASE WHEN to_date('2021-06-01','yyyy-MM-dd') > t.bytime and t.bytime >= to_date('2021-03-01','yyyy-MM-dd') then 1 else 0 end as score
,
(select COUNT(1) from BUYTICKETS) AS ALLNUM
FROM BUYTICKETS t
UNION ALL
SELECT '第三季度' as jidu ,
CASE WHEN to_date('2021-09-01','yyyy-MM-dd') > t.bytime and t.bytime >= to_date('2021-06-01','yyyy-MM-dd') then 1 else 0 end as score
,
(select COUNT(1) from BUYTICKETS) AS ALLNUM
FROM BUYTICKETS t
UNION ALL
SELECT '第四季度' as jidu ,
CASE WHEN to_date('2021-12-01','yyyy-MM-dd') >= t.bytime and t.bytime >= to_date('2021-09-01','yyyy-MM-dd') then 1 else 0 end as score
,
(select COUNT(1) from BUYTICKETS) AS ALLNUM
FROM BUYTICKETS t) bb group by bb.jidu,bb.allnum;