Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Remaining Time Calculation Expand / Collapse
Author
Message
Posted Friday, July 24, 2009 4:42 AM
SSC Rookie

SSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC RookieSSC Rookie

Group: General Forum Members
Last Login: Wednesday, October 15, 2014 5:24 AM
Points: 49, Visits: 526
Hi All,

How to calculate remaining time

like one day my shecudle is :


DATE APPFROMTIME APP TO TIME
07/24/2009 12:18:00 PM 1:18:00 PM
07/24/2009 14:30:00 PM 15:30:00 PM
07/24/2009 17:30:00 PM 18:30:00 PM


but i want remaing free APPtime like....................


DATE APPFROMTIME APP TO TIME
07/24/2009 12:00:00 AM 12:17:00 PM
07/24/2009 1:17:00 PM 14:29:00 PM
07/24/2009 16:31:00 PM 17:29:00 PM
07/24/2009 18:31:00 PM 11:59:00 PM


plz help me.........................

Post #758928
Posted Friday, July 24, 2009 12:27 PM
SSC Veteran

SSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC VeteranSSC Veteran

Group: General Forum Members
Last Login: Wednesday, August 13, 2014 3:50 PM
Points: 292, Visits: 1,623
Can you explaine how you calcualted those values? I'm totaly missing how you go them.
Post #759405
Posted Friday, July 24, 2009 4:07 PM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Wednesday, October 24, 2012 2:12 PM
Points: 1,212, Visits: 3,232
I haven't quite reproduced your required results, but then I don't believe that those results are really what you want!

Here is the TSQL to set up some test data. If test data is presented in the form of CREATE TABLE and INSERT statements it makes it simpler for other people to set up the test data on their systems, and you are more likely to get a quick response.

CREATE TABLE #Appointment (
AppDate datetime,
AppFromTime datetime,
AppToTime datetime
)
GO

INSERT INTO #Appointment (AppDate, AppFromTime, AppToTime)
SELECT '20090724', '12:18:00', '13:18:00' UNION ALL
SELECT '20090724', '14:30:00', '15:30:00' UNION ALL
SELECT '20090724', '17:30:00', '18:30:00' UNION ALL
SELECT '20090725', '09:30:00', '10:15:00' UNION ALL
SELECT '20090725', '13:15:00', '14:00:00' UNION ALL
SELECT '20090726', '00:00:00', '02:00:00'

The query will return the time intervals not occupied by appointments, and also split up into separate days between the date/time range defined by @StartTime and @EndTime.

DECLARE @StartTime datetime
DECLARE @EndTime datetime

SELECT @StartTime = '2009-07-24T00:00:00'
SELECT @EndTime = '2009-07-27T00:00:00'

;WITH cteSchedule (rn, StartTime, EndTime) AS (
SELECT
ROW_NUMBER() OVER (ORDER BY S.StartTime, S.EndTime),
S.StartTime,
S.EndTime
FROM (
SELECT
AppDate + AppFromTime AS StartTime,
AppDate + AppToTime AS EndTime
FROM #Appointment
UNION ALL
SELECT
DATEADD(day, T.N, @StartTime),
DATEADD(day, T.N, @StartTime)
FROM dbo.Tally T
WHERE (T.N BETWEEN 0 AND DATEDIFF(day, @StartTime, @EndTime))
) S
)
SELECT
S1.EndTime AS FreeTimeStart,
S2.StartTime AS FreeTimeEnd
FROM cteSchedule S1
INNER JOIN cteSchedule S2 ON (S1.rn = S2.rn - 1)
WHERE (S1.EndTime < S2.StartTime)
ORDER BY S1.rn

The query uses a Tally (aka Numbers) table of sequential integers beween 0 and at least the number of days in the date range.
Post #759538
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse