Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2001-06-19
14
Medium Priority
?
367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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 400 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
In this article, we’ll look at how to deploy ProxySQL.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

598 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