Sql(在Oracle上)按天计算老化报告

前端之家收集整理的这篇文章主要介绍了Sql(在Oracle上)按天计算老化报告前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我需要帮助写一篇关于oracle的老化报告.
报告应该像:
aging file to submit total       17
 aging file to submit 0-2 days    3
 aging file to submit 2-4 days    4
 aging file to submit 4-6 days    4
 aging file to submit 6-8 days    2 
 aging file to submit 8-10 days   4

我可以为每个部分创建一个查询,然后将所有结果联合起来,如:

select 'aging file to submit total  ' || count(*) from FILES_TO_SUBMIT where trunc(DUE_DATE) > trunc(sysdate) -10
union all
select 'aging file to submit 0-2 days ' || count(*) from FILES_TO_SUBMIT where trunc(DUE_DATE) <= trunc(sysdate)  and trunc(DUE_DATE) >= trunc(sysdate-2)
union all
select 'aging file to submit 2-4 days ' || count(*) from FILES_TO_SUBMIT where trunc(DUE_DATE) <= trunc(sysdate-2) and trunc(DUE_DATE) >= trunc(sysdate-4) ;

我想知道是否有更好的方法使用oracle分析函数或任何其他可以获得更好性能查询

样本数据:

CREATE TABLE files_to_submit(file_id int,file_name varchar(255),due_date date); 

INSERT INTO FILES_TO_SUBMIT(FILE_ID,FILE_NAME,DUE_DATE) VALUES  ( 1,'file_' || 1,sysdate);
INSERT INTO FILES_TO_SUBMIT(FILE_ID,DUE_DATE) VALUES  ( 2,'file_' || 2,sysdate -5);
INSERT INTO FILES_TO_SUBMIT(FILE_ID,DUE_DATE) VALUES  ( 3,'file_' || 3,sysdate -4);
INSERT INTO FILES_TO_SUBMIT(FILE_ID,DUE_DATE) VALUES  ( 4,'file_' || 4,DUE_DATE) VALUES  ( 5,'file_' || 5,sysdate-3);
INSERT INTO FILES_TO_SUBMIT(FILE_ID,DUE_DATE) VALUES  ( 6,'file_' || 6,sysdate-7);
INSERT INTO FILES_TO_SUBMIT(FILE_ID,DUE_DATE) VALUES  ( 7,'file_' || 7,sysdate-10);
INSERT INTO FILES_TO_SUBMIT(FILE_ID,DUE_DATE) VALUES  ( 8,'file_' || 8,sysdate-12);
INSERT INTO FILES_TO_SUBMIT(FILE_ID,DUE_DATE) VALUES  ( 9,'file_' || 9,DUE_DATE) VALUES  ( 10,'file_' || 10,sysdate-5);
INSERT INTO FILES_TO_SUBMIT(FILE_ID,DUE_DATE) VALUES  ( 11,'file_' || 11,sysdate-6);
INSERT INTO FILES_TO_SUBMIT(FILE_ID,DUE_DATE) VALUES  ( 12,'file_' || 12,DUE_DATE) VALUES  ( 13,'file_' || 13,DUE_DATE) VALUES  ( 14,'file_' || 14,sysdate-4);
INSERT INTO FILES_TO_SUBMIT(FILE_ID,DUE_DATE) VALUES  ( 15,'file_' || 15,sysdate-2);
INSERT INTO FILES_TO_SUBMIT(FILE_ID,DUE_DATE) VALUES  ( 16,'file_' || 16,DUE_DATE) VALUES  ( 17,'file_' || 17,DUE_DATE) VALUES  ( 18,'file_' || 18,DUE_DATE) VALUES  ( 19,'file_' || 19,DUE_DATE) VALUES  ( 20,'file_' || 20,sysdate-9);


DROP TABLE files_to_submit;

解决方法

请允许我建议 WIDTH_BUCKET.
这会将日期范围划分为相同的大小.由于您希望将10天范围分为2天组,因此桶大小将为10/2 = 5.

查询

SELECT 
    CASE GROUPING(bucket) 
        WHEN 1 
            THEN 'aging file to submit Total' 
            ELSE 'aging file to submit ' || (bucket-1)*2 || '-' || (bucket)*2 || ' days'
    END             AS bucket_number,COUNT(1)        AS files
FROM (
    SELECT 
        WIDTH_BUCKET(due_date,sysdate,sysdate-10,5) bucket 
    FROM 
        files_to_submit
    WHERE 
        due_date >= sysdate-10
    )
GROUP BY
    ROLLUP(bucket)
ORDER BY
    bucket NULLS FIRST;

结果:

BUCKET_NUMBER                             FILES
------------------------------------ ----------
aging file to submit Total                   17
aging file to submit 0-2 days                 2
aging file to submit 2-4 days                 3
aging file to submit 4-6 days                 6
aging file to submit 6-8 days                 5
aging file to submit 8-10 days                1
原文链接:https://www.f2er.com/mssql/76104.html

猜你在找的MsSQL相关文章