简体   繁体   English

SQL查询根据传递的参数返回1-31的缺勤日期

[英]SQL Query to return absence date from 1-31 according to parameter passed

I have table我有桌子

PERSON_NUMBER       ABS_DATE            ABS_TYPE_NAME               ABS_DAYS
1010            01-01-2022              PTO                             1
1010            06-01-2022              PTO                             0.52
1010            02-02-2022              VACATION                        1
1010            03-02-2022              VACATION                        0.2
1010            01-12-2021              PTO                             1
1010            02-12-2021              sick                            1                   
1010            30-12-2021              sick                            1
1010            30-01-2022              SICK                            1

when using below query it gives me -使用以下查询时,它给了我-

SELECT *
FROM
( 
    SELECT PERSON_NUMBER,
           EXTRACT(DAY FROM TO_DATE(ABS_DATE)) AS DAY_X,
           ABS_TYPE_NAME,
           ABS_DAYS
    FROM TABLE
    -- Add additional filter here which you want
 ) 
PIVOT(SUM(ABS_DAYS)
         FOR DAY_X IN (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31))
         

output comes as -输出为 -

PERSON_NUMBER          ABS_TYPE_NAME    1  2   3 4 5 6    7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31   
    1010                    PTO             2            0.52
    1010                    VACATION           1   0.2
    1010                    SICK                                           1                                                        2
    
    

Now i want to add parameter quarter, year and month现在我想添加参数季度、年和月

  1. Quarter will be passed as 2022 Q 1, 2022 Q 2, 2022 Q 3 etc季度将通过 2022 Q 1、2022 Q 2、2022 Q 3 等

  2. Month will be passed as 01/2022, 02/2022, 03/2022, 04/2022 ETC.月份将通过 01/2022、02/2022、03/2022、04/2022 ETC。

  3. Year will be passed as 2021, 2022年份将通过 2021, 2022

When I am passing year,quarter -当我过年、季——

SELECT *
    FROM
    ( select * from(
        SELECT PERSON_NUMBER,
               EXTRACT(DAY FROM TO_DATE(ABS_DATE)) AS DAY_X,
               ABS_TYPE_NAME,
               ABS_DAYS,
               abs_date,
               NVL(to_date(substr(:p_quarter, 1, 4) || case substr(:p_quarter, -1)  when '1' then '03'
                                                                       when '2' then '06'
                                                                       when '3' then '09'
                                                                       when '4' then '12'
                                                                end, 'yyyymm'),ABS_DATE) p_quarter
        FROM abs_table
        )
            where to_char(abs_date,'yyyy') in NVL(:P_YEAR,to_char(sysdate,'yyyy'))
            and abs_date between p_quarter and last_day(p_quarter)
     ) 
    PIVOT(SUM(ABS_DAYS)
             FOR DAY_X IN (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31))
    
    

But this query is processing endlessly and not generating any output.但是这个查询正在无休止地处理并且没有产生任何输出。 How can i tweak this to make it working and not get stuck我该如何调整它以使其正常工作而不被卡住

Changed the p_quarter column creation and between condition in where clause.在 where 子句中更改了 p_quarter 列的创建和条件之间。 It works here.它在这里工作。 You should just make your own representation of P_QUARTER result column.您应该只对 P_QUARTER 结果列进行自己的表示。 Regards...问候...

SELECT *  
    FROM
    ( select * from(
        SELECT PERSON_NUMBER,
               EXTRACT(DAY FROM TO_DATE(ABS_DATE)) AS DAY_X,
               ABS_TYPE_NAME,
               ABS_DAYS,
               abs_date,
               NVL(to_date(substr(:p_quarter, 1, 4) || case substr(:p_quarter, -1)  when '1' then '0301'
                                                                       when '2' then '0601'
                                                                       when '3' then '0901'
                                                                       when '4' then '1201'
                                                                end, 'yyyymmdd'),ABS_DATE) p_quarter
        FROM table1
        )
            where to_char(abs_date,'yyyy') in NVL(:P_YEAR,to_char(sysdate,'yyyy'))
            and abs_date between add_months(p_quarter, -3) and last_day(p_quarter)
     ) 
    PIVOT(SUM(ABS_DAYS)
             FOR DAY_X IN (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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