Pregunta

I have a table that logs when user enters and leaves a specific room during the course of a day using a swipe system.

I am attempting to create a more visual based report based on the data stored in our SQL Server which is in the below format:

EventTime       |UserID   |SubAddress|PeripheralName  |EventTypeID|Serial number
08/05/2014 08:24|128      |1         |Upstairs  (In)  |20         |547929
08/05/2014 08:35|128      |2         |Upstairs  (Out) |20         |547929
08/05/2014 08:37|128      |1         |Upstairs  (In)  |20         |547929
08/05/2014 09:50|128      |2         |Upstairs  (Out) |20         |547929

From here what ideally I need to do is create a way of showing when they where out of the office (in minutes). I have found some code that, if i am honest I don't entirely understand and haven't been able to modify to work.

DECLARE @StartDate Datetime
DECLARE @EndDate Datetime
Declare @MinDiff int
Declare @ConsStDate datetime
Declare @ConsEndDate datetime

set @StartDate = '2010-07-02 14:02' --Start time for Report
set @EndDate = '2010-07-02 16:02' --End time for Report
set @MinDiff = Datediff(Mi,@StartDate,@EndDate) --Local use

set @ConsStDate = '2010-07-02 14:22' --Consulation start time
set @ConsEndDate = '2010-07-02 15:52' --Consulation end time


--INSERT @MyTable (Value,LogTime)
SELECT Convert(varchar,DATEADD(mi,number,@StartDate),108) 
          , Case When DATEADD(mi,number,@StartDate) between @ConsStDate and @ConsEndDate Then 1 else 0 End as 'With Patient'
          FROM master..spt_values
                where [type]='p'
                     and number BETWEEN 1 AND @MinDiff

If anyone has done something similar i would appreciate any assistance on what I should be doing going forward.

Desired example of output (roughly)

EventTime       | Status
08/05/2014 08:24    | IN
08/05/2014 08:25    | OUT
08/05/2014 08:26    | OUT
08/05/2014 08:27    | OUT
08/05/2014 08:28    | OUT
08/05/2014 08:29    | IN
¿Fue útil?

Solución

Okay, so since you need one record output per minute, you will need a way to generate a record for each minute between some interval (@StartDate - @EndDate):

DECLARE @StartDate Datetime
DECLARE @EndDate Datetime
set @StartDate = '2010-07-02 14:02' --Start time for Report
set @EndDate = '2010-07-02 16:02' --End time for Report

select
    DateAdd(MINUTE, Number, @StartDate) AS Minutes
from    
    (   
        -- This is just a fancy way to generate a list of numbers:
        select row_number() over (order by t1.object_id) AS Number
        from sys.all_objects t1, sys.all_objects t2
    ) AS Numbers
where Number <= DateDiff(MINUTE, @StartDate, @EndDate)

With the code above, you get a list of all the minutes between the chosen interval.

Now what you need to do, is to join the output from above, with your event table. I won't provide the code for this here, but this should get you started.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top