How to count overlaps of a time range and update the table with that information

I have a table that in the following structure:

seqid       int
UserId      char(3)
BegTime     datetime
EndTime     datetime
Overlaps    smallint

I need an sql statement that looks at each record in the table, and updates the Overlaps field with a count of the number of records that overlap the time records time range, when the UserId is the same.

For instance, if I start with the following table data (assuming date portion is same):

1      abc       8:00     10:00
2      abc       9:40     10:02
3      abc     10:01     10:30
4      def        8:00     10:30

the resulting table should be:

1      abc       8:00     10:00      1
2      abc       9:40     10:02      2
3      abc     10:01     10:30      1
4      def        8:00     10:30      0

I hope this explains it clearly enough.
jmcfaddenAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Guy Hengel [angelIII / a3]Connect With a Mentor Billing EngineerCommented:
In fact, for such kind of complex things, i first do the select before i use it to update the statement, i assumed you could transform it yourself...

UPDATE YourTable
SET Overlaps = ( SELECT COUNT(*) FROM YourTable O
   WHERE O.UserID = T.UserID
     AND O.seqid <> T.seqID
     AND ( O.BegTime BETWEEN T.BegTime AND T.EndTime
         OR O.EndTime BETWEEN T.BegTime AND T.EndTime
         OR T.BegTime BETWEEN O.BegTime AND O.EndTime
         OR T.EndTime BETWEEN O.BegTime AND O.EndTime
         )
   )

Cheers
0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
select
 seqid ,
 UserId ,
 BegTime ,    
 EndTime ,
 overlaps = ( SELECT COUNT(*) FROM YourTable O
    WHERE O.UserID = T.UserID
      AND O.seqid <> T.seqID
      AND ( O.BegTime BETWEEN T.BegTime AND T.EndTime
          OR O.EndTime BETWEEN T.BegTime AND T.EndTime
          OR T.BegTime BETWEEN O.BegTime AND O.EndTime
          OR T.EndTime BETWEEN O.BegTime AND O.EndTime
          )
    )
FROM
 YourTable T

Cheers
0
 
nigelrivettCommented:
don't need the last 'or' I feel (but doesn't hurt).

select
seqid ,
UserId ,
BegTime ,    
EndTime ,
overlaps = ( SELECT COUNT(*) FROM YourTable O
   WHERE O.UserID = T.UserID
     AND O.seqid <> T.seqID
     AND ( O.BegTime BETWEEN T.BegTime AND T.EndTime
         OR O.EndTime BETWEEN T.BegTime AND T.EndTime
         OR T.BegTime BETWEEN O.BegTime AND O.EndTime
         )
   )
FROM
YourTable T
0
Improve Your Query Performance Tuning

In this FREE six-day email course, you'll learn from Janis Griffin, Database Performance Evangelist. She'll teach 12 steps that you can use to optimize your queries as much as possible and see measurable results in your work. Get started today!

 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
you are right, and i had added it only for the easier understanding of the conditions. To see/feel that it is unnesessary shows you are a real expert (which i know!)
:-)

Cheers
0
 
jmcfaddenAuthor Commented:
Unless I am not reading the answers correctly, neither will actually update the table.
0
 
jmcfaddenAuthor Commented:
I had to abbreviate the BEGTIME and ENDTIME fields to BT and ET, I renamed OVERLAPS to OVRLAP since "OVERLAPS" is a reserved SQL Keyword, and I eliminated the UserId check to make it more simple for the moment. So here is the statement I am running:

UPDATE TESTING SET OVRLAP = ( SELECT COUNT(*) FROM TESTING O WHERE O.USERID = T.USERID AND (O.BT BETWEEN T.BT AND T.ET OR O.ET BETWEEN T.BT AND T.ET OR T.BT BETWEEN O.BT AND O.ET OR T.ET BETWEEN O.BT AND O.ET ) ) FROM TESTING T

This looks to me from your example that it should work, however I get the error message "Syntax error (missing operator) in query expression '( SELECT COUNT(*) FROM TESTING O WHERE O.USERID = T.USERID AND (O.BT BETWEEN T.BT AND T.ET OR O.ET BETWEEN T.BT AND T.ET OR T.BT BETWEEN O.BT AND O.ET OR T.ET BETWEEN O.BT AND O.ET ) ) FROM TESTING T'.

And that is all the information it gives me. Any suggestions?
0
 
nigelrivettCommented:
Change )) to ).
0
 
nigelrivettCommented:
Oops

change
) ) FROM TESTING T
to
)  FROM TESTING T)
0
 
jmcfaddenAuthor Commented:
Nope, gives me the same syntax error.

I have again tried to simplify it even further to help narrow down the problem. Although this would not give me the correct values, I thought it might help to eliminate part of the query, and give some clue as to where the problem was. It didn't. The following queries still gives me the same syntax error.

UPDATE TESTING SET OVRLAP = ( SELECT COUNT(*) FROM TESTING O WHERE O.USERID = T.USERID) FROM TESTING T

UPDATE TESTING SET OVRLAP = ( SELECT COUNT(*) FROM TESTING O WHERE O.USERID = T.USERID FROM TESTING T)

Any other ideas?
0
 
nigelrivettCommented:
try
UPDATE TESTING SET OVRLAP = (SELECT COUNT(*) FROM TESTING O WHERE O.USERID = TESTING.USERID)
0
 
nigelrivettCommented:
This shold work too but looks similar to your original.
(sorry didn't realise what you wanted to do).

UPDATE TESTING SET OVRLAP = (SELECT COUNT(*) FROM TESTING O WHERE O.USERID = T.USERID)
from TESTING T


now think your original should have worked
0
 
nigelrivettCommented:
n.b. this works on my system - try it on yours.

create table #a (i int, j int)

insert #a select 1,1
insert #a select 2,1

create table #b (i int, j int)

insert #b select 1,1
insert #b select 1,1
insert #b select 1,1
insert #b select 1,1
insert #b select 2,1
insert #b select 2,1
insert #b select 2,1


update #a set j = (select count(*) from #b b where a.i = b.i) from #a a

select * from #a

drop table #a
drop table #b
0
 
nigelrivettCommented:
Could it be you need update T instead?
0
 
jmcfaddenAuthor Commented:
Here is something interesting to note. I decided to import the table into SQL Server (from MSAccess), and guess what? The SQL statement works.

So, it is apparently something specific to MSAccess that is causing it to fail. So the statement is correct, and what I needed, it just won't work under MSAccess.

Thanks for everyone's time and effort.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.