0

如何使以下查询从季度结束后的第一天检索结果

换言之,第 1 季度的结果应包括 4 月 1 日及以后的记录,第 2 季度的结果应包括 7 月 1 日及以后的记录,依此类推

select year, quarter, uploaddate, wagecount, 
       row_number()
         OVER (partition by year, quarter
               ORDER BY quarter, uploaddate) as Day_Number 
from 
(
select year, quarter, uploaddate, sum(wagecount) as wagecount from SAMPLE_DATA
group by year, quarter, uploaddate
)
order by year, quarter, uploaddate

我尝试了以下操作,但运行查询时没有记录

select year, quarter, uploaddate, wagecount, 
       row_number()
         OVER (partition by year, quarter
               ORDER BY quarter, uploaddate) as Day_Number 
from 
(
select year, quarter, uploaddate, sum(wagecount) as wagecount from SAMPLE_DATA
where uploaddate > add_months(trunc(uploaddate,'Q'),3)-1
group by year, quarter, uploaddate
)
order by year, quarter, uploaddate;

样本数据如下:

CREATE TABLE SAMPLE_DATA (
    YEAR VARCHAR2(4) NULL,
    QUARTER NUMBER(1,0) NULL,
    UPLOADDATE DATE NULL,
    WAGECOUNT NUMBER(10,0) NULL
);

Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2019','1',to_date('27-MAR-19','DD-MON-RR'),5);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2019','1',to_date('28-MAR-19','DD-MON-RR'),8493);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2019','1',to_date('29-MAR-19','DD-MON-RR'),15070);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2019','1',to_date('30-MAR-19','DD-MON-RR'),1244);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2019','1',to_date('29-APR-19','DD-MON-RR'),13000);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2019','1',to_date('30-APR-19','DD-MON-RR'),1024);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2020','1',to_date('03-JAN-20','DD-MON-RR'),0);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2020','1',to_date('05-JAN-20','DD-MON-RR'),2);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2020','1',to_date('06-JAN-20','DD-MON-RR'),3);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2020','1',to_date('07-JAN-20','DD-MON-RR'),6);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2021','2',to_date('21-APR-21','DD-MON-RR'),59);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2021','2',to_date('22-APR-21','DD-MON-RR'),10);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2021','2',to_date('23-APR-21','DD-MON-RR'),16);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2021','2',to_date('24-APR-21','DD-MON-RR'),1);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2021','2',to_date('23-JUL-21','DD-MON-RR'),106);
Insert into SAMPLE_DATA (YEAR,QUARTER,UPLOADDATE,WAGECOUNT) values ('2021','2',to_date('24-JUL-21','DD-MON-RR'),41);
4

0 回答 0