Solved

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

Posted on 2001-06-19
14
347 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 143

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 143

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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

Author Comment

by:jmcfadden
ID: 6210255
Unless I am not reading the answers correctly, neither will actually update the table.
0
 
LVL 143

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

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DBF to ... Converter 5 83
SSRS troubles 4 70
Need help constructing a conditional update query 16 71
Software for Local History Museum Library 5 54
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!
Shadow IT is coming out of the shadows as more businesses are choosing cloud-based applications. It is now a multi-cloud world for most organizations. Simultaneously, most businesses have yet to consolidate with one cloud provider or define an offic…
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…

821 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