How to create T-SQL by hour between a TIME_IN and TIME_OUT columns

Posted on 2010-01-07
Last Modified: 2012-05-08
I'm using Microsoft SQL Server 2005 and 2008.

SAY The table only has a STUDENT NAME, TIME_IN, TIME_OUT columns.

If Paul takes a class from 8am-3pm and John takes a class from 9am-noon, I want to create an HOURLY report that looks like:


8-9am, 1
9-10am, 2
10-11am, 2
11am-12pm, 2
12pm-1pm, 1
1pm-2pm, 1
2pm-3pm, 1
Question by:mscptran
    LVL 10

    Expert Comment

    have a look at the sample code

    You may use CTE as I've done it now or you may have a table with all hours.
    For simplicity I'm not checking all the hours in this snippet but you will see the mechanics.
    LVL 10

    Expert Comment

    Something happened with the code. here it goes again
    create table Students (studentId int identity, studentName varchar(32), studentIn tinyint, studentOut tinyint)
    insert into Students values ('Paul', 8, 15)
    insert into Students values ('John', 9, 12)
    with AllHours as
    	select 6 as H
    	union select 7
    	union select 8
    	union select 9
    	union select 10
    	union select 11
    	union select 12
    	union select 13
    	union select 14
    	union select 15
    select AH.H, COUNT(*) from AllHours AH
    left outer join Students S on AH.H between S.studentIn and S.studentOut-1
    group by AH.H

    Open in new window

    LVL 4

    Accepted Solution

    --populate x table: Assumes date range cannot be more than 24 hrs
    --this is usually a permanent table
    declare @x table
    (x          int)

    declare @counter int
    set @counter = 0

    while @counter < 25
        insert into @x (x) values (@counter)
        set @counter = @counter + 1

    --populate/get student records
    declare @base table
    (studentname   varchar(50)
    ,begindate     datetime
    ,enddate       datetime)

    insert into @base (studentname, begindate, enddate)
    values ('Paul', '01/07/2010 08:00', '01/07/2010 15:00')
    ,('John', '01/07/2010 09:00', '01/07/2010 12:00')

    --parse into hourly records
    select dateadd(hh,x,begindate) as starttime, dateadd(hh,x+1,begindate) as endtime, count(studentname) as [number of students]
    from @base b
    join @x on 1 = 1
    where dateadd(hh,x,begindate) < enddate
    group by dateadd(hh,x,begindate), dateadd(hh,x+1,begindate)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Enabling OSINT in Activity Based Intelligence

    Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

    Hi All, I am here to write a simple article to move SSRS (SQL Server Reporting Services) reports from one server to another. When I have faced the same issue to move reports those were developed by developer on development server and now need to …
    Hi all, It is important and often overlooked to understand “Database properties”. Often we see questions about "log files" or "where is the database" and one of the easiest ways to get general information about your database is to use “Database p…
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
    Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now