SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Remaining Time Calculation


Remaining Time Calculation

Author
Message
polo.csit
polo.csit
Right there with Babe
Right there with Babe (794 reputation)Right there with Babe (794 reputation)Right there with Babe (794 reputation)Right there with Babe (794 reputation)Right there with Babe (794 reputation)Right there with Babe (794 reputation)Right there with Babe (794 reputation)Right there with Babe (794 reputation)

Group: General Forum Members
Points: 794 Visits: 680
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.........................
Lamprey13
Lamprey13
SSCrazy
SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)SSCrazy (2.2K reputation)

Group: General Forum Members
Points: 2178 Visits: 1657
Can you explaine how you calcualted those values? I'm totaly missing how you go them.
andrewd.smith
andrewd.smith
Hall of Fame
Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)Hall of Fame (3.6K reputation)

Group: General Forum Members
Points: 3636 Visits: 3232
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.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search