Solved

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

Posted on 2001-06-19
14
339 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]
ID: 6207481
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
ID: 6208001
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]
ID: 6209253
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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:jmcfadden
ID: 6210255
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
ID: 6211099
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
ID: 6228159
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
ID: 6230691
Change )) to ).
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6230693
Oops

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

Author Comment

by:jmcfadden
ID: 6231177
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
ID: 6231254
try
UPDATE TESTING SET OVRLAP = (SELECT COUNT(*) FROM TESTING O WHERE O.USERID = TESTING.USERID)
0
 
LVL 18

Expert Comment

by:nigelrivett
ID: 6231278
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
ID: 6231283
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
ID: 6231296
Could it be you need update T instead?
0
 

Author Comment

by:jmcfadden
ID: 6232753
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Never store passwords in plain text or just their hash: it seems a no-brainier, but there are still plenty of people doing that. I present the why and how on this subject, offering my own real life solution that you can implement right away, bringin…
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…

816 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

12 Experts available now in Live!

Get 1:1 Help Now