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
 General SQL Server Forums
 New to SQL Server Programming
 Get a list of enquiries updated in last 24hrs?

Author  Topic 

Maverick_
Posting Yak Master

107 Posts

Posted - 2010-03-02 : 10:39:23
Hi all!

I am trying to create a query that gives me a list of customer enquiries that were updated in the last 24hrs but the query also needs to exclude weekends and holidays.

By "update" I mean their "status" changed.

There is a Status table that holds status details related to enquiry: enquiry_number, logged_date <-- contains the date and time of the status being changed.

The enquiry table has enquiry related details such as: enquiry_number, enquiry_desc, enquiry_time etc.

How do I create a query that shows me only those enquiries that had their statuses changed in the last 24hrs AND exclude weekends and holidays?

I don't quite know how to format a query that does both :(

Any help would be greatly appreciated!

M

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-02 : 10:44:55
do you mean exclude weekends and holidays for 24 hrs window calculation or exclude them for status change condition?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-02 : 10:47:13
Also you need to have a table in your db that contains holiday info

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Maverick_
Posting Yak Master

107 Posts

Posted - 2010-03-02 : 10:53:09
Hi Visakh hope you are well!

Yes indeed there is a Holiday table which contains: nonworking_day_id, nonworking_date, nonworking_day_name.

And you are right I should have clarified this as well, I mean exclude them for 24hr window calculation. So examples of holidays could include 25/12/2010, 28/12/2010, 01/01/2011 etc.
Go to Top of Page

Maverick_
Posting Yak Master

107 Posts

Posted - 2010-03-02 : 11:26:37
I am trying to figure out how the db is going to realise what counts as a "weekend" and the thing is, the database I am working with - I cannot create tables in it where I could enter e.g. 2010 weekend dates. Hm
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-03-02 : 11:43:55
Well I am surprised you are working on a system where you dont have rights to even create table.But if you have access to master db then you can create a temp calendar table using a system lookup table.

select DATEADD(DD,number,'01-01-2010') from dbo.spt_values where type='P'
and DATENAME(dw, DATEADD(DD,number,'01-01-2010') )not in ('Saturday','Sunday')

Sachin Nandanwar

PBUH
Go to Top of Page

Sachin.Nand

2937 Posts

Posted - 2010-03-02 : 11:44:49
By the way how do you define your weekends ?
Sachin Nandanwar

PBUH
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-02 : 11:57:49
quote:
Originally posted by Maverick_

I am trying to figure out how the db is going to realise what counts as a "weekend" and the thing is, the database I am working with - I cannot create tables in it where I could enter e.g. 2010 weekend dates. Hm


create a function like this


IF EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_NAME ='BusinessHours' AND ROUTINE_SCHEMA = 'dbo' AND ROUTINE_TYPE='FUNCTION')
DROP FUNCTION dbo.BusinessHours
GO
CREATE FUNCTION dbo.BusinessHours
(
@StartDate datetime,
@EndDate datetime
)
RETURNS int
AS
BEGIN
DECLARE @TotalHours int
;With Calendar_CTE (Date,Day,WeekDay)
AS
(
SELECT DATEADD(dd,DATEDIFF(dd,0,@StartDate),0),DATENAME(dw,DATEADD(dd,DATEDIFF(dd,0,@StartDate),1)), CASE WHEN DATENAME(dw,DATEADD(dd,DATEDIFF(dd,0,@StartDate),1)) NOT IN ('Saturday','Sunday') THEN 1 ELSE 0 END

UNION ALL

SELECT DATEADD(dd,1,Date),DATENAME(dw,DATEADD(dd,1,Date)), CASE WHEN DATENAME(dw,DATEADD(dd,1,Date)) NOT IN ('Saturday','Sunday') THEN 1 ELSE 0 END
FROM Calendar_CTE
WHERE DATEADD(dd,1,Date) <= DATEADD(dd,DATEDIFF(dd,0,@EndDate),0)
)


SELECT @TotalHours=SUM(
CASE WHEN c.Date=DATEADD(dd,DATEDIFF(dd,0,@StartDate),0) THEN DATEDIFF(MINUTE,@StartDate,DATEADD(dd,DATEDIFF(dd,0,@StartDate),1))
WHEN c.Date=DATEADD(dd,DATEDIFF(dd,0,@EndDate),0) THEN DATEDIFF(MINUTE,DATEADD(dd,DATEDIFF(dd,0,@EndDate),0),@EndDate)
ELSE 24*60
END
)/60
FROM Calendar_CTE c
LEFT JOIN Holiday h
ON h.[date]= c.Date
WHERE WeekDay=1
AND h.[date] IS NULL
OPTION (MAXRECURSION 0)

RETURN @TotalHours
END


then use it like below

SELECT e.enquiry_number, e.enquiry_desc, e.enquiry_time
FROM enquiry e
INNER JOIN status s
ON s.enquiry_number=e.enquiry_number
WHERE dbo.BusinessHours(s.logged_date ,GETDATE()) < = 24


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

Maverick_
Posting Yak Master

107 Posts

Posted - 2010-03-02 : 11:58:37
Sachin this is exactly what I have been trying to solve for the past hour, and I don't think there is a way around this. So even if I get the figure to exclude just the holidays it will be enough.

I know this is not a proper format but so far my query looks like this:

SELECT
ce.enquiry_number,
ce.enquiry_desc,
esl.logged_date

FROM
customer_enquiry ce,
enquiry_status_log esl,
holiday h

WHERE
ce.enquiry_number = esl.enquiry_number

EDIT:

Visakh I am going to give that a go and see what happens!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-03-02 : 12:11:14
Sure and let me know how you got on

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -