Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Calculate no. of working days

Author  Topic 

zany
Starting Member

4 Posts

Posted - 2007-03-07 : 14:15:28
Hi,

I want to select all the orders where the difference between the ordered date and the delivered date is greater than 5 working days. I wrote a query which will do this. But I don't think this is the optimized way to get the job done.

can anyone suggest a better solution?

Tables
TableA has Ordered Date
TableB has Delivered Date
TableC has holiday list (columns: HolidayName, Date )

Here is my query

select A.OrderID, Datediff ( dd, A.OrderedDate, B.DeliveredDate) as NumberOfDays
from TableA A left join TableB B
on A.OrderID = B.OrderID
where Datediff ( dd, A.OrderedDate, B.DeliveredDate) - (Select count(*) From TableC Where [Date] between A.OrderedDate and B.DeliveredDate) >= 5

Thanks in advance

pareshmotiwala
Constraint Violating Yak Guru

323 Posts

Posted - 2007-03-07 : 14:55:52
zany, why do you think this is not cool? you could use function but that would be an overkill for this one....
Go to Top of Page

zany
Starting Member

4 Posts

Posted - 2007-03-07 : 15:19:41
Thank you for the reply Paresh. I was just hoping there could be a better solution.
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-07 : 17:08:41
what is your definition of working days ?


KH

Go to Top of Page

zany
Starting Member

4 Posts

Posted - 2007-03-07 : 17:14:05
All days excluding Saturdays, Sundays and holidays
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2007-03-07 : 17:20:48
You can make use of F_GET_WORK_DAYS from http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=80158&SearchTerms=F_GET_WORK_DAYS to calculate the no of working days between OrderedDate & DeliveredData and then subtract any holidays in-between


KH

Go to Top of Page
   

- Advertisement -