473,378 Members | 1,333 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,378 software developers and data experts.

How to find out difference between two rows....

Dear Gurus,

I have below table with data

No.| Agent Start_Time End_Time
==========================================
1 | 194 2008-05-30 10:19:00 2008-05-30 10:23:06.000
2 | 194 2008-05-30 10:39:00 2008-05-30 10:41:59.000
3 | 194 2008-05-30 10:44:00 2008-05-30 11:11:45.000
4 | 194 2008-05-30 11:17:00 2008-05-30 11:22:19.000
5 | 194 2008-05-30 11:46:00 2008-05-30 12:55:56.000
6 | 194 2008-05-30 13:02:00 2008-05-30 13:56:34.000
7 | 194 2008-05-30 14:36:00 2008-05-30 14:36:08.000
8 | 194 2008-05-30 17:41:00 2008-05-30 18:18:26.000

I want to find Idle time duration as follows

for record no. 1 =>
Idle time = (start_time of record no. 2) - (End_time of record no. 1)

for record no. 2 =>
Idle time = (start_time of record no. 3) - (End_time of record no. 2)

and so on.

Means output as follows
No.| Agent Start_Time End_Time
Idle_Time (in min or sec)
================================================== ===========
1 | 194 2008-05-30 10:19:00 2008-05-30 10:23:06.000 15:54 min
2 | 194 2008-05-30 10:39:00 2008-05-30 10:41:59.000 02:01 min
3 | 194 2008-05-30 10:44:00 2008-05-30 11:11:45.000 05:15 min
4 | 194 2008-05-30 11:17:00 2008-05-30 11:22:19.000 .......
5 | 194 2008-05-30 11:46:00 2008-05-30 12:55:56.000 .......
6 | 194 2008-05-30 13:02:00 2008-05-30 13:56:34.000 .......
7 | 194 2008-05-30 14:36:00 2008-05-30 14:36:08.000 ......
8 | 194 2008-05-30 17:41:00 2008-05-30 18:18:26.000 00:00 min
So Is there any formula/inbuilt function available in SQL Server 2000

Could any one help me for above problem ?

Thanking in advance
Sanjeev
Aug 14 '08 #1
4 11299
1) Please learn why rows are not records.
2) Please post real DDL and not narrative.
3) Add an activity_code column which includes 'idle' as one of its
values. This will give you what you want now and can expand when you
want more details.
Aug 14 '08 #2
Roy Harvey (SQL Server MVP) (ro********@snet.net) writes:
What version of Microsoft SQL Server are you running?
Actually, he said SQL 2000. :-)

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

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinf...ons/books.mspx

Aug 14 '08 #3
On Thu, 14 Aug 2008 21:49:14 +0000 (UTC), Erland Sommarskog
<es****@sommarskog.sewrote:
>Actually, he said SQL 2000. :-)
Oops!

Thanks, Erland.

Roy Harvey
Beacon Falls, CT
Aug 14 '08 #4
--- Sanjeev <sa**************@gmail.comwrote:
Table def :
Create Table Callbycall(
Agent int,
Start_Time datetime,
End_Time datetime
)

and required output as follows

Agent Start_Time End_Time
Idle_Time(derived column)
================================================== =========
194 2008-05-30 10:19:00 2008-05-30 10:23:06.000 15:54
194 2008-05-30 10:39:00 2008-05-30 10:41:59.000 02:01
194 2008-05-30 10:44:00 2008-05-30 11:11:45.000 05:15
194 2008-05-30 11:17:00 2008-05-30 11:22:19.000 23:41
194 2008-05-30 11:46:00 2008-05-30 12:55:56.000 00:00
200 2008-05-30 13:02:00 2008-05-30 13:56:34.000 39:26
200 2008-05-30 14:36:00 2008-05-30 14:36:00.000 64:00
200 2008-05-30 15:40:00 2008-05-30 15:58:20.000 .....
200 2008-05-30 13:02:00 2008-05-30 13:56:34.000 00:00
.................
.................
.................
380 2008-05-30 13:02:00 2008-05-30 13:56:34.000 39:26
380 2008-05-30 14:36:00 2008-05-30 14:36:00.000 64:00
380 2008-05-30 15:40:00 2008-05-30 15:58:20.000 .....
380 2008-05-30 13:02:00 2008-05-30 13:56:34.000 00:00
The first issue is calculating the seconds.

SELECT *,
DATEDIFF(second, A.End_Time,
(SELECT MIN(Start_Time)
FROM Callbycall as B
WHERE A.Agent = B.Agent
AND A.Start_Time < B.End_Time)) as ElapsedSecond
FROM Callbycall as A

One way to format the time expression, which you should be able to
adapt to your needs:

declare @i int
set @i = 12345
SELECT convert(char(8), dateadd(second, @i, 0), 114)

--------
03:25:45

Roy Harvey
Beacon Falls, CT
Aug 15 '08 #5

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

Similar topics

3
by: lkrubner | last post by:
Suppose I make a call to MySql and zero rows come back. How do I tell the difference between zero rows and failure?
1
by: Shane Niebergall | last post by:
Can someone explain why there is such a big performance difference in these two queries? Note: I have a multicolumn index on ltid and inuse. mysql> UPDATE leads SET inuse='0' WHERE inuse!='0' and...
16
by: Justin Hoffman | last post by:
This is a question concerning query optimisation. Sorry if it's a bit long, but thanks to anyone who has the patience to help - This is my first post here... If I have two tables:...
8
by: Paulo Jan | last post by:
Hi all: I have here a table with the following schema: Table "todocinetv" Column | Type | Modifiers...
0
by: Ashish | last post by:
Hi, I am not able to find out the Delta XML. Requirement Spec: If I have a file with <ID>101</ID> <Name>BOB</Name> I want to add the changes made to the file like rows added , removed and...
5
by: BBFrost | last post by:
Win2000 ..Net 1.1 SP1 c# using Visual Studio Ok, I'm currently in a "knock down - drag out" tussle with the .Net 1.1 datagrid. I've come to realize that a 'block' of rows highlighted within...
3
by: Solel Software | last post by:
Hello, I have a basic question. I have a DataTable of information without a database store (it's only in memory). I am looking to somehow query the DataTable to find out which row(s) satisfy...
17
by: Justin Emlay | last post by:
I'm hopping someone can help me out on a payroll project I need to implement. To start we are dealing with payroll periods. So we are dealing with an exact 10 days (Monday - Friday, 2 weeks). ...
67
by: PC Datasheet | last post by:
Transaction data is given with date ranges: Beginning End 4/1/06 4/4/06 4/7/06 4/11/06 4/14/06 4/17/06 4/18/06 4/21/06 426/06 ...
4
by: Jane T | last post by:
I appreciate how difficult it is to resolve a problem without all the information but maybe someone has come across a similar problem. I have an 'extract' table which has 1853 rows when I ask for...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
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: 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: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.