473,385 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Calculate time lapse

js
Can anyone help with the following Transact SQL question? Thanks. I
need a store procedure to return the the result recordset which will be
execute from a web page. The database has tables, A and B. For each A
record, there are many related B records. In the B table there is a
timestamp field which tracks the change of A record. For example, A1
has B like the followings:

ID TimeStamp Chg Code Descption
== ========= ======= ========
A1 1138375875 E null //end of the event
A1 1138025002 S resume
A1 1137092615 S don't care
A1 1137092570 S stop
A1 1137092256 I null //start of the
event

I need to generate all records in table A and total elapse time for
each record, but B with Chg Code 'S' that has "don't cacre" to be
deducted from the total time, so that the result will be like this:

ID Name TotalTime
(seconds)
== ==== =======
A1 xyz 351187

Apr 5 '06 #1
5 3985
das
try this, I haven't tested it:

select A.ID, A.Name, Sum(B.TimeStamp)
from A inner join B on A.ID = B.ID
group by A.ID, A.Name
having (B.ChgCode <> 'S' and B.Description <> 'don''t care')

adi

Apr 5 '06 #2
js (an********@yahoo.com) writes:
Can anyone help with the following Transact SQL question? Thanks. I
need a store procedure to return the the result recordset which will be
execute from a web page. The database has tables, A and B. For each A
record, there are many related B records. In the B table there is a
timestamp field which tracks the change of A record. For example, A1
has B like the followings:

ID TimeStamp Chg Code Descption
== ========= ======= ========
A1 1138375875 E null //end of the event
A1 1138025002 S resume
A1 1137092615 S don't care
A1 1137092570 S stop
A1 1137092256 I null //start of the
event

I need to generate all records in table A and total elapse time for
each record, but B with Chg Code 'S' that has "don't cacre" to be
deducted from the total time, so that the result will be like this:

ID Name TotalTime
(seconds)
== ==== =======
A1 xyz 351187


It is not clear to how that "don't care" row is to be deducted, since
that is just a point in time. Had you posted CREATE TABLE statements
for the tables, and INSERT statements with the data, it would have been
easy to play around. The below is just a guess, and is untested:

SELECT B.ID, SUM(elapsed)
FROM (SELECT B1.ID, elapsed = B1.TimeStamp - B2.Timestamp,
B2.ChgCode, B2.Description
FROM B B1
JOIN B B2 ON B1.ID = B2.ID
AND B2.TimeStamp = (SELECT MAX(B3.Timestamp)
FROM B B3
WHERE B3.ID = B1.ID
AND B3.TimeStamp <
B1.TimeStamp)
) AS B
WHERE NOT (B.ChgCode = 'S' AND B2.ChgCode = 'don''t care')
GROUP BY B.ID

Here, I'm making the assumption that it is the time from the don't-
care event until the next event that is to be ignored.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 5 '06 #3
js
Thanks for the reply. The TimeStamp column is the actual time the
event occurs not the length of each event, so summing the column will
not give me the actual length of the time from the start to end. The
calculation should be
(time event starts - time envent ends) - (time resume starts - time
resume stops)
thus the result is
(1138375875 - 1137092256) - (1138025002 - 1137092570) = 351187

Any idea? I think I have to use cursor to loop through each result
block and determine if the Description field contains 'stop' or
'resume'. Thanks.

Apr 5 '06 #4
js
Thank you. It works with minor modification. Now I would like to use
a trigger so that upon insert the elapsed time will be posted in Table
A column (int) "TimeLapse". However, it would not accept the value.
Can you help?

ALTER TRIGGER [updateA]
ON [dbo].[b]
AFTER INSERT
AS
BEGIN
SET NOCOUNT ON;

Update A
set A.TimeLapse = (SELECT SUM(elapsed)
FROM (SELECT B1.ID, B1.[TimeStamp] -
B2.[TimeStamp] AS elapsed, B2.ChgCode, B2.description
FROM B B1 INNER JOIN
B B2 ON B1.ID = B2.ID AND
B2.[TimeStamp] =
(SELECT
MAX(B3.Timestamp)
FROM B B3
WHERE B3.ID = B1.ID
AND B3.TimeStamp < B1.TimeStamp)
WHERE not (B1.ChgCode = 'S' and
(b1.description
like '%resume%' or b1.description like '%don''t care%')) OR
(b1.description IS
NULL)) B GROUP BY ID)
END

Apr 6 '06 #5
js (an********@yahoo.com) writes:
Thank you. It works with minor modification. Now I would like to use
a trigger so that upon insert the elapsed time will be posted in Table
A column (int) "TimeLapse". However, it would not accept the value.
Can you help?


You must correlate the computation of elapsed with a row in A. The
easiest way is to use the proprietary FROM/JOIN syntax supported by
MS SQL Server:

Update A
set TimeLapse = Btot.elapsed
FROM A
JOIN (SELECT B.ID, elapsed = SUM(elapsed)
FROM (SELECT B1.ID, B1.[TimeStamp] - B2.[TimeStamp] AS elapsed,
B2.ChgCode, B2.description
FROM B B1
JOIN B B2 ON B1.ID = B2.ID
AND B2.[TimeStamp] =
(SELECT MAX(B3.Timestamp)
FROM B B3
WHERE B3.ID = B1.ID
AND B3.TimeStamp < B1.TimeStamp)
WHERE not (B1.ChgCode = 'S' and
(b1.description like '%resume%' or
b1.description like '%don''t care%'))
OR (b1.description IS NULL)) B
GROUP BY B.ID) AS Btot = A.ID = B.ID

However, neither this is entierly satisfactory, as you are reading the
entire B table a couple of times on each insert, and this could be
expensive. SQL Server offers the the virtual tables "inserted" and
"deleted" which holds after-image and before-images of the rows
affected by the statement. (For an INSERT, there are only rows in
"inserted" obviously.)

Rewriting the trigger to look at inserted is not trivial, least of all
if rows can be inserted out of order. (What if a "don't care" row is
inserted in the middle of it all?)

Not knowing the exact scenario where this appears I prefer to not suggest
a solution.

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Apr 6 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Mike A | last post by:
Hi, I'm hoping someone can help me with this. I have a URL for which I'd like to limit access to by time. For example,say I have a URL that I don't want accessable on Monday mornings between...
1
by: sg_s123 | last post by:
============================================================================ 02-Feb-04 03-Feb-04 Staff Staff 0800hr- 1300hr- 1700hr- 1900hr- 0800hr- 1300hr- 1700hr- 1900hr- Number...
7
by: Tim Quon | last post by:
Hi Is there any function to get the current time so I can calculate the execution time of my code? What all is in the time.h and sys/times.h? Thanks Tim
6
by: Herrcho | last post by:
in K&R Chapter 6.3 it mentions two methods to calculate NKEYS. and points out the first one which is to terminate the list of initializers with a null pointer, then loop along keytab until the...
15
by: Fergus Cooney | last post by:
Hi Everyone, I finished playing newgroupie this morning and went to bed. When I came back, I couldn't get any new messages for this group. I'm using Outlook Express. It showed the newsgroup...
2
by: drurjen | last post by:
Good morning. I am importing an XLS file into one of my tables. The fields are: Date Id Time IO 12/22/2006 2 12:48:45 PM 9 12/22/2006 16 5:40:55 AM 1 12/22/2006 16 12:03:59 PM 2 ...
5
by: Beemer Biker | last post by:
I cant seem to get that date into any DateTime to make my calculation directly by subtracting "01-01-0000" from "now". After reading this:...
2
by: Lancelot | last post by:
Hello everyone. This is my first post here, but I've been looking for answer many time. I have a crazy idea in my head and since I am quite a newby to python, I am not sure where to start or if...
6
by: rrstudio2 | last post by:
I am using the following vba code to calculate the median of a table in MS Access: Public Function MedianOfRst(RstName As String, fldName As String) As Double 'This function will calculate the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.