how to get working days each month [message #159386] |
Fri, 17 February 2006 23:31  |
jchiu183
Messages: 16 Registered: September 2005
|
Junior Member |
|
|
How can I change the follow query to get the working days each month without manually enter the value in '&1' and '&2'?
select count(*)
2 from ( select rownum rnum
3 from all_objects
4 where rownum <= to_date('&1') - to_date('&2')+1 )
5 where to_char( to_date('&2')+rnum-1, 'DY' )
not in ( 'SAT', 'SUN' );
Thank you!
|
|
|
|
Re: how to get working days each month [message #159389 is a reply to message #159388] |
Sat, 18 February 2006 00:35   |
 |
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
In a different flavour:
SQL> SELECT themonth
2 , count(theday) cnt
3 FROM ( SELECT TO_CHAR(TRUNC(SYSDATE,'YY')+LEVEL-1,'Month') themonth
4 , TO_CHAR(TRUNC(SYSDATE,'YY')+LEVEL-1,'DY') theday
5 FROM dual
6 CONNECT BY LEVEL <= ADD_MONTHS(TRUNC(SYSDATE,'YY'),12)-TRUNC(SYSDATE,'YY')
7 )
8 WHERE theday NOT IN ('SAT','SUN')
9 GROUP BY themonth
10 ORDER BY TO_DATE(themonth,'MM')
11 /
THEMONTH CNT
--------- ----------
January 22
February 20
March 23
April 20
May 23
June 22
July 21
August 23
September 21
October 22
November 22
December 21
12 rows selected.
MHE
|
|
|
|
|
Re: how to get working days each month [message #160073 is a reply to message #160017] |
Thu, 23 February 2006 01:24   |
 |
Maaher
Messages: 7065 Registered: December 2001
|
Senior Member |
|
|
The same principle applies: You use a connect by row generator.
Now we need to count the all days but weekend days. A SUM with DECODE is one option:
SQL> SELECT SUM( -- transform the generated date to abbreviated day of week
2 DECODE( TO_CHAR(TRUNC(thedate,'MM')+LEVEL-1,'DY')
3 , 'SAT', 0 -- Saturdays don't count
4 , 'SUN', 0 -- Sundays don't count
5 , 1 -- all other days count
6 )
7 ) sales_day_no
8 , thedate
9 FROM ( SELECT TO_DATE('22/02/2006','DD/MM/YYYY') thedate
10 FROM dual
11 )
12 -- row generator technique from 1/2/2006 to 22/2/2006
13 CONNECT BY TRUNC(thedate,'MM')+LEVEL-1 <= thedate
14 /
SALES_DAY_NO THEDATE
------------ ---------
16 22-FEB-06 Some people prefer a case statement. If I were you, I'd put it in a (deterministic) function for readability. Let me demonstrate. Here's a small test script.
CREATE OR REPLACE FUNCTION get_sales_day_no(pid_date IN DATE)
RETURN NUMBER DETERMINISTIC
IS
v_return NUMBER;
BEGIN
SELECT SUM( -- transform the generated date to abbreviated day of week
DECODE( TO_CHAR(TRUNC(pid_date,'MM')+LEVEL-1,'DY')
, 'SAT', 0 -- Saturdays don't count
, 'SUN', 0 -- Sundays don't count
, 1 -- all other days count
)
) sales_day_no
INTO v_return
FROM dual
-- row generator technique
CONNECT BY TRUNC(pid_date,'MM')+LEVEL-1 <= pid_date;
RETURN v_return;
END get_sales_day_no;
/
sho err
SELECT TO_DATE('22/02/2006','dd/mm/yyyy') yourdate
, get_sales_day_no(TO_DATE('22/02/2006','dd/mm/yyyy')) sales_day_no
FROM dual
/
DROP FUNCTION get_sales_day_no
/
It's output is this:
SQL> @C:\useful\orafaq
Function created.
No errors.
YOURDATE SALES_DAY_NO
--------- ------------
22-FEB-06 16
Function dropped.
MHE
|
|
|
|