Welcome to the { mindfrost82.com } forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact us.

Go Back   { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > Databases > General SQL Server Support

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-14-2008, 08:19 AM
Sanjeev
 
Posts: n/a
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
Reply With Quote
  #2 (permalink)  
Old 08-14-2008, 02:18 PM
Roy Harvey (SQL Server MVP)
 
Posts: n/a
Re: How to find out difference between two rows....

What version of Microsoft SQL Server are you running?

Do the time ranges ever overlap? Does the calculation have to be by
Agent, or is it across all Agent values? Is there a constraint in the
system to insure that when the "No." column is in order the times are
also in order? When you say 15:54 min is the 15 hours and 54 minutes,
or 15 minutes and 54 seconds?

One simple approach that might not be right, or best, depending on
information not given.

SELECT *,
DATEDIFF(second, A.End_Time,
(SELECT MIN(Start_Time)
FROM Whatever as B
WHERE A.Start_Time < B.End_Time)) as ElapsedSecond
FROM Whatever as A

Roy Harvey
Beacon Falls, CT

On Thu, 14 Aug 2008 00:19:54 -0700 (PDT), Sanjeev
<sanjeev.atvankar@gmail.com> wrote:

>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

Reply With Quote
  #3 (permalink)  
Old 08-14-2008, 07:45 PM
--CELKO--
 
Posts: n/a
Re: How to find out difference between two rows....

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.
Reply With Quote
  #4 (permalink)  
Old 08-14-2008, 10:49 PM
Erland Sommarskog
 
Posts: n/a
Re: How to find out difference between two rows....

Roy Harvey (SQL Server MVP) (roy_harvey@snet.net) writes:
> What version of Microsoft SQL Server are you running?


Actually, he said SQL 2000. :-)



--
Erland Sommarskog, SQL Server MVP, esquel@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

Reply With Quote
  #5 (permalink)  
Old 08-14-2008, 11:47 PM
Roy Harvey (SQL Server MVP)
 
Posts: n/a
Re: How to find out difference between two rows....

On Thu, 14 Aug 2008 21:49:14 +0000 (UTC), Erland Sommarskog
<esquel@sommarskog.se> wrote:

>Actually, he said SQL 2000. :-)


Oops!

Thanks, Erland.

Roy Harvey
Beacon Falls, CT
Reply With Quote
  #6 (permalink)  
Old 08-15-2008, 01:36 PM
Roy Harvey (SQL Server MVP)
 
Posts: n/a
Re: How to find out difference between two rows....

--- Sanjeev <sanjeev.atvankar@gmail.com> wrote:

> 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
Reply With Quote
Reply

  { mindfrost82.com } > Gadget Corner > Tech Newsgroups > Programming > Databases > General SQL Server Support


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT. The time now is 04:41 PM.


Powered by vBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
© 1999-2008 mindfrost82.com v11.0


Sponsors:
Electricity Suppliers | Credit Report | WoW Gold | Credit Cards | Personal Loans



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114