Solved

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

Posted on 2001-06-19
14
319 Views
Last Modified: 2006-11-17
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.
0
Comment
Question by:jmcfadden
  • 7
  • 4
  • 3
14 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
 
LVL 18

Expert Comment

by:nigelrivett
Comment Utility
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
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
Comment Utility
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
 

Author Comment

by:jmcfadden
Comment Utility
Unless I am not reading the answers correctly, neither will actually update the table.
0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 100 total points
Comment Utility
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
 

Author Comment

by:jmcfadden
Comment Utility
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
 
LVL 18

Expert Comment

by:nigelrivett
Comment Utility
Change )) to ).
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 18

Expert Comment

by:nigelrivett
Comment Utility
Oops

change
) ) FROM TESTING T
to
)  FROM TESTING T)
0
 

Author Comment

by:jmcfadden
Comment Utility
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
 
LVL 18

Expert Comment

by:nigelrivett
Comment Utility
try
UPDATE TESTING SET OVRLAP = (SELECT COUNT(*) FROM TESTING O WHERE O.USERID = TESTING.USERID)
0
 
LVL 18

Expert Comment

by:nigelrivett
Comment Utility
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
 
LVL 18

Expert Comment

by:nigelrivett
Comment Utility
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
 
LVL 18

Expert Comment

by:nigelrivett
Comment Utility
Could it be you need update T instead?
0
 

Author Comment

by:jmcfadden
Comment Utility
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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
SQL Command Tool comes with APEX under SQL Workshop. It helps us to make changes on the database directly using a graphical user interface. This helps us writing any SQL/ PLSQL queries and execute it on the database and we can create any database ob…
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

18 Experts available now in Live!

Get 1:1 Help Now