如何找到一个季度的最后一个工作日?没有周六/周日。假期很好。
下面给出了上一季度的最后一天,但我需要找到最后一个工作日。
SELECT TRUNC(SYSDATE, 'Q') - 1 LAST_DAY_OF_LAST_QTR
FROM DUAL
答案 0 :(得分:1)
我确信有一个更优雅的解决方案,但这就是我想出来的:
以下是我对您的系统的假设:
with data(r) as
(select 0 r from dual
union all
select r+1 from data where r < 6
)
select max(countingdays) lastbusinessdayofqtr
from (
select trunc(sysdate, 'Q') - 1 - r countingdays,
case when to_char(trunc(sysdate, 'Q') - 1 - r,'D') between 2 and 6 then 'Y' else 'N' end BusinessDay
from data)
where businessday = 'Y';
with语句生成从0到6的数字行。我使用这些数字从季度的最后一天减去。 2至6日之间的任何日子是周一至周五。最后,我采用businessday为'Y'的最长日期。
答案 1 :(得分:1)
此独立于当前用户NLS_TERRITORY
和NLS_DATE_LANGUAGE
设置:
select decode(to_char(trunc(sysdate,'Q')-1,'Dy', 'NLS_DATE_LANGUAGE = american'),
'Sat',trunc(sysdate,'Q')-2, -- for saturday
'Sun',trunc(sysdate,'Q')-3, -- for sunday
trunc(sysdate,'Q')-1 -- for other days
) LAST_DAY_OF_LAST_QTR from dual;
答案 2 :(得分:0)
这是一个微不足道的解决方案,
如果您的季度最后一天是星期日,您将返回2天,或者如果星期六,您将返回1天。然后,借助 SELECT
pics.piclocation,
pics.picid,
SUM(rating) as total_rating
FROM
pics,
ratings
WHERE
pics.milfid = ratings.picid
GROUP BY
ratings.picid
ORDER BY
total_rating DESC
函数,您可以使用:
decode
假设您获得了值SQL> col LAST_DAY_OF_LAST_QTR format a22
SQL> select decode(to_char(trunc(sysdate,'Q')-1,'D'),
7,trunc(sysdate,'Q')-2, -- for saturday
1,trunc(sysdate,'Q')-3, -- for sunday
trunc(sysdate,'Q')-1 -- for other days
) LAST_DAY_OF_LAST_QTR from dual;
,1 for Sunday
关于以下查询中7 for saturday
的本地设置:
nls_date_language
答案 3 :(得分:0)
这个怎么样?,6是查询中的星期五。
SELECT NEXT_DAY(TRUNC(SYSDATE, 'Q') - 7, 6) LAST_FRIDAY_OF_LAST_QTR
FROM DUAL;
最后一天,请参阅下面的3个示例查询。
17是星期日(1)和星期六(7)。如果星期日/星期六是最后一天,那么它将在最后一个星期五返回,否则返回最后一天。
SELECT DECODE(INSTR('17', TO_CHAR(TRUNC(TO_DATE('01-JAN-2019', 'DD-MON-YYYY'), 'Q')-1, 'D'))
, 0, TRUNC(TO_DATE('01-JAN-2019', 'DD-MON-YYYY'), 'Q')-1,
NEXT_DAY(TRUNC(TO_DATE('01-JAN-2019', 'DD-MON-YYYY'), 'Q') - 7, 6)
) LAST_BUSINESS_DAY_OF_LAST_QTR
FROM DUAL;
SELECT DECODE(INSTR('17', TO_CHAR(TRUNC(TO_DATE('01-AUG-2019', 'DD-MON-YYYY'), 'Q')-1, 'D'))
, 0, TRUNC(TO_DATE('01-AUG-2019', 'DD-MON-YYYY'), 'Q')-1,
NEXT_DAY(TRUNC(TO_DATE('01-AUG-2019', 'DD-MON-YYYY'), 'Q') - 7, 6)
) LAST_BUSINESS_DAY_OF_LAST_QTR
FROM DUAL;
SELECT DECODE(INSTR('17', TO_CHAR(TRUNC(TO_DATE('01-OCT-2019', 'DD-MON-YYYY'), 'Q')-1, 'D'))
, 0, TRUNC(TO_DATE('01-OCT-2019', 'DD-MON-YYYY'), 'Q')-1,
NEXT_DAY(TRUNC(TO_DATE('01-OCT-2019', 'DD-MON-YYYY'), 'Q') - 7, 6)
) LAST_BUSINESS_DAY_OF_LAST_QTR
FROM DUAL;