dbTalk Databases Forums  

this cannot be then best way

comp.databases.oracle.misc comp.databases.oracle.misc


Discuss this cannot be then best way in the comp.databases.oracle.misc forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
 
Posts: n/a

Default this cannot be then best way - 07-14-2006 , 11:38 AM






I have a date mydate and i want to subtract X working days off

i was thinking of something like

loop while x > 0
if to_char(mydate,'d') < 6 then
x = x -1
endif
mydate :mydate - 1
endloop

any ideas



Reply With Quote
  #2  
Old   
Ed Prochak
 
Posts: n/a

Default Re: this cannot be then best way - 07-14-2006 , 01:33 PM







g wrote:
Quote:
I have a date mydate and i want to subtract X working days off

i was thinking of something like

loop while x > 0
if to_char(mydate,'d') < 6 then
x = x -1
endif
mydate :mydate - 1
endloop

any ideas
If X tends to be large (>2weeks),
then save a few passes thru the loop by

--- computing the whole weeks
whole_weeks := ( 7* (X/5) ) --- must use integer arithmetic
mydate := mydate - whole_weeks
x := mod(x,5)
--- then put your loop here:


HTH,
ed



Reply With Quote
  #3  
Old   
akkha1234 (AT) gmail (DOT) com
 
Posts: n/a

Default Re: this cannot be then best way - 07-14-2006 , 01:47 PM



What if you have some holiday in between?

Ed Prochak wrote:
Quote:
g wrote:
I have a date mydate and i want to subtract X working days off

i was thinking of something like

loop while x > 0
if to_char(mydate,'d') < 6 then
x = x -1
endif
mydate :mydate - 1
endloop

any ideas

If X tends to be large (>2weeks),
then save a few passes thru the loop by

--- computing the whole weeks
whole_weeks := ( 7* (X/5) ) --- must use integer arithmetic
mydate := mydate - whole_weeks
x := mod(x,5)
--- then put your loop here:


HTH,
ed


Reply With Quote
  #4  
Old   
Ed Prochak
 
Posts: n/a

Default Re: this cannot be then best way - 07-14-2006 , 04:09 PM




akkha1234 (AT) gmail (DOT) com wrote:
Quote:
What if you have some holiday in between?

I'm thinking he handles that afterwards.

select count(*) from holidays where holiday_date between mydatestart
and mydate ;

Yes there are boundary issues, but faster than looping for every day.
(again this is all optimization for workdays X being relatively large
most of the time this operation is called.

As always in performance tuning, the first key is: know your data.

Ed



Reply With Quote
  #5  
Old   
DA Morgan
 
Posts: n/a

Default Re: this cannot be then best way - 07-14-2006 , 04:52 PM



g wrote:
Quote:
I have a date mydate and i want to subtract X working days off

i was thinking of something like

loop while x > 0
if to_char(mydate,'d') < 6 then
x = x -1
endif
mydate :mydate - 1
endloop

any ideas
Just a quick note: TO_CHAR does not produce a number and
essentially none of the code, above, is valid. It will
never run in Oracle. What product do you have?
--
Daniel A. Morgan
University of Washington
damorgan@x.washington.edu
(replace x with u to respond)
Puget Sound Oracle Users Group
www.psoug.org


Reply With Quote
  #6  
Old   
AT
 
Posts: n/a

Default Re: this cannot be then best way - 07-14-2006 , 05:31 PM



g wrote:
: I have a date mydate and i want to subtract X working days off

: i was thinking of something like

: loop while x > 0
: if to_char(mydate,'d') < 6 then
: x = x -1
: endif
: mydate :mydate - 1
: endloop

two ideas come to mind

-1-

Keep a table that lists the dates of the holidays and weekends dates for
the year (call them off_days)

first_guess = mydate - x/5*7 # account for weekends

non_working = (select count(off_days) between guess and mydate)
new_guess = mydate - non_working
iterate until the new_guess doesn't change


-2-

fill a table with every work day of the year, and for each day save the
number of days worked up to that day (the_date, worked_days)

given mydate,
lookup the number of days worked in the year up to that date
select worked_days from year where the_date= mydate
subtract x to find days we want to have worked (call it wanted)
select the date that has that many days worked
select the_date from year where worked_days = wanted

$0.10



Reply With Quote
  #7  
Old   
Vladimir M. Zakharychev
 
Posts: n/a

Default Re: this cannot be then best way - 07-16-2006 , 03:50 AM



DA Morgan wrote:
Quote:
g wrote:
I have a date mydate and i want to subtract X working days off

i was thinking of something like

loop while x > 0
if to_char(mydate,'d') < 6 then
x = x -1
endif
mydate :mydate - 1
endloop

any ideas

Just a quick note: TO_CHAR does not produce a number and
essentially none of the code, above, is valid. It will
never run in Oracle. What product do you have?
It will, as PL/SQL engine will implicitly apply TO_NUMBER() to the
result of TO_CHAR() in attempt to match types of the values being
compared. Not a good idea to rely on implicit type conversion though
(at least until 10g where it seems to be more efficient than explicit
conversion sometimes.)

Regards,
Vladimir M. Zakharychev
N-Networks, makers of Dynamic PSP(tm)
http://www.dynamicpsp.com



Reply With Quote
  #8  
Old   
Ed Prochak
 
Posts: n/a

Default Re: this cannot be then best way - 07-16-2006 , 08:18 AM




Vladimir M. Zakharychev wrote:
Quote:
DA Morgan wrote:
g wrote:
I have a date mydate and i want to subtract X working days off

i was thinking of something like

loop while x > 0
if to_char(mydate,'d') < 6 then
x = x -1
endif
mydate :mydate - 1
endloop

any ideas

Just a quick note: TO_CHAR does not produce a number and
essentially none of the code, above, is valid. It will
never run in Oracle. What product do you have?

It will, as PL/SQL engine will implicitly apply TO_NUMBER() to the
result of TO_CHAR() in attempt to match types of the values being
compared. Not a good idea to rely on implicit type conversion though
(at least until 10g where it seems to be more efficient than explicit
conversion sometimes.)

Regards,
Vladimir M. Zakharychev
I took it as pseudocode as it will not work as written. Just look at
the assignment statements.



Reply With Quote
  #9  
Old   
 
Posts: n/a

Default Re: this cannot be then best way - 07-17-2006 , 05:45 AM




"Ed Prochak" <edprochak (AT) gmail (DOT) com> wrote

Quote:
Vladimir M. Zakharychev wrote:
DA Morgan wrote:
g wrote:
I have a date mydate and i want to subtract X working days off

i was thinking of something like

loop while x > 0
if to_char(mydate,'d') < 6 then
x = x -1
endif
mydate :mydate - 1
endloop

any ideas

Just a quick note: TO_CHAR does not produce a number and
essentially none of the code, above, is valid. It will
never run in Oracle. What product do you have?

It will, as PL/SQL engine will implicitly apply TO_NUMBER() to the
result of TO_CHAR() in attempt to match types of the values being
compared. Not a good idea to rely on implicit type conversion though
(at least until 10g where it seems to be more efficient than explicit
conversion sometimes.)

Regards,
Vladimir M. Zakharychev

I took it as pseudocode as it will not work as written. Just look at
the assignment statements.

Thanks all
it works a treat
it always worth a second look to see if anyone else has a better idea





Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.