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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Grouping By Specified Time Period

Author  Topic 

sandsingh
Starting Member

11 Posts

Posted - 2008-12-08 : 03:41:01
Team:

I need your help with what I feel is a complicated query for me. Here's the data I am working with:

UserID IncidentNo. DateTime
User1 XPI1243 12/8/2008 9:00:01 PM
User2 XPI1243 12/8/2008 9:45:01 PM
User2 XPI1243 12/8/2008 10:20:35 PM
User1 XPI1243 12/8/2008 11:55:00 PM
User1 XPI1243 12/9/2008 1:12:10 AM
User2 XPI1243 12/9/2008 5:05:41 AM
User1 XPI1243 12/9/2008 5:55:01 AM
User1 XPI1243 12/9/2008 9:00:01 PM
User2 XPI1243 12/9/2008 9:45:01 PM
User2 XPI1243 12/9/2008 10:20:35 PM
User1 XPI1243 12/9/2008 11:55:00 PM
User1 XPI1243 12/10/2008 1:12:10 AM
User2 XPI1243 12/10/2008 5:05:41 AM
User1 XPI1243 12/10/2008 5:55:01 AM

The working shift for user1 and user2 is 9 pm to 6 am everyday. Is there a way that I can use the select query to group the number of requests processed by each user shift-wise (9 pm to 6 am) and day-wise? My problem is the overlapping of dates during one shift - for example, a user begins the working shift on 12/8 9 pm and ends it on 12/9 6 am.

Please help! Thank you so much.

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2008-12-08 : 10:45:25
One way is to create a table (or some sort of derived table as below) to define the shifts. Then JOIN to that table:

--Your Sample data
declare @t table (UserID varchar(10), IncidentNo varchar(10), DateTime [datetime])
insert @t
select 'User1', 'XPI1243', '12/8/2008 9:00:01 PM' union all
select 'User2', 'XPI1243', '12/8/2008 9:45:01 PM' union all
select 'User2', 'XPI1243', '12/8/2008 10:20:35 PM' union all
select 'User1', 'XPI1243', '12/8/2008 11:55:00 PM' union all
select 'User1', 'XPI1243', '12/9/2008 1:12:10 AM' union all
select 'User2', 'XPI1243', '12/9/2008 5:05:41 AM' union all
select 'User1', 'XPI1243', '12/9/2008 5:55:01 AM' union all
select 'User1', 'XPI1243', '12/9/2008 9:00:01 PM' union all
select 'User2', 'XPI1243', '12/9/2008 9:45:01 PM' union all
select 'User2', 'XPI1243', '12/9/2008 10:20:35 PM' union all
select 'User1', 'XPI1243', '12/9/2008 11:55:00 PM' union all
select 'User1', 'XPI1243', '12/10/2008 1:12:10 AM' union all
select 'User2', 'XPI1243', '12/10/2008 5:05:41 AM' union all
select 'User1', 'XPI1243', '12/10/2008 5:55:01 AM'

select * from @t where userid = 'user1' order by [datetime]

;with ShiftGroup (grp, startShift, endShift)
as ( --Create a table with your Shifts for each day
select v.number grp
,dateadd(hour, 21, dateadd(day, number, mn)) startShift
,dateadd(hour, 6, dateadd(day, number+1, mn)) endShift
from (
select datediff(day, mn, mx) days
,mn
,mx
from (
select dateadd(day, datediff(day, 0, min([datetime])), 0) mn
,dateadd(day, datediff(day, 0, max([datetime])), 0) mx
from @t
) d
) d
inner join master..spt_values v
on v.type = 'P'
and v.number <= days
)

--Final Select GROUPed by Shift
select t.UserID
,sg.startShift
,sg.endShift
,count(*) [requestCount]
from ShiftGroup sg
inner join @t t
on t.[datetime] >= sg.startShift
and t.[datetime] <= sg.endshift
group by t.UserID
,sg.startShift
,sg.endShift



OUTPUT:
UserID startShift endShift requestCount
---------- ----------------------- ----------------------- ------------
User1 2008-12-08 21:00:00.000 2008-12-09 06:00:00.000 4
User1 2008-12-09 21:00:00.000 2008-12-10 06:00:00.000 4
User2 2008-12-08 21:00:00.000 2008-12-09 06:00:00.000 3
User2 2008-12-09 21:00:00.000 2008-12-10 06:00:00.000 3


Be One with the Optimizer
TG
Go to Top of Page

sandsingh
Starting Member

11 Posts

Posted - 2008-12-13 : 12:30:11
Wow TG, thanks a lot for your help! I am sorry but I have quite a vague idea about queries, and apologies if I sound very dumb to you. The query I am looking for goes into one of the report forms that I am developing in Visual Basic 2005. Something like this:

Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft Jet 4.0 OLE DB Provider;Data Source=filepath\DocServ.mdb;Jet " _
& "OLEDB:Database Password=filepassword")
Dim cmd As OleDbCommand = New OleDbCommand("Select * From UtilizationTable_ds where LoginID='" & GetUserName() & "' and ForTheDate between #" & ttREPORTSFromDate.Value & "# and #" & ttREPORTSToDate.Value & "# order by forthedate desc", con)
con.Open()
Dim myDA As OleDbDataAdapter = New OleDbDataAdapter(cmd)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "UtilizationTable_ds")
TTDataGridView1.DataSource = myDataSet.Tables("UtilizationTable_ds").DefaultView
con.Close()


Therefore, I am not the best person to implement your wonderful solution as I am not sure how to use it. Will the query you provided go as a single line of code, or is it a procedure? If it's a procedure, is there a way you can help me out with a query? I know I am asking for too much; but can you help me out with this? Thanks again!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-12-13 : 12:35:09
make the query as a stored procedure and call it from vb application

http://www.nigelrivett.net/VB/VBExecSPSimple.html
Go to Top of Page

sandsingh
Starting Member

11 Posts

Posted - 2008-12-13 : 17:09:42
Stored procedure in an mdb?
Go to Top of Page
   

- Advertisement -