Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Calculating Total Time

Author  Topic 

jpiscit1
Posting Yak Master

130 Posts

Posted - 2005-05-12 : 08:31:56
I posted this once before and didn’t get any resolution. I am assuming I did not explain it well and that its not to complicated for this group to answer.

I will take another shot at it.

I have a table structure as follows:

CREATE TABLE [dbo].[tbl_1PMProduction] (
[RecordDate_Time] [datetime] NULL ,
[StockPumpRunning] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[CouchShtBrk] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[WBDiscoRunning] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL
) ON [PRIMARY]
GO

For the purposes of this request, lets say it contains the following data over a 1 hour period:

Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:33:52.400','OFF', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:32:52.400','OFF', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:31:52.400','OFF', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:30:52.400','OFF', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:29:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:28:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:27:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:26:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:25:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:24:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:23:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:22:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:21:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:20:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:19:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:18:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:17:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:16:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:15:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:14:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:13:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:12:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:11:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:10:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:09:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:08:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:07:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:06:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:05:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:04:52.400','RUNNING', 'BREAK', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:03:52.400','RUNNING', 'BREAK', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 11:02:52.400','RUNNING', 'BREAK', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 10:38:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 10:38:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 10:37:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 10:36:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 10:35:52.400','RUNNING', 'CLEAR', 'RUNNING')
Insert into tbl_1PMProduction (RecordDate_Time, WBDiscoRunning, CouchShtBrk, StockPumpRunning)
values ('2005-05-09 10:34:52.400','RUNNING', 'CLEAR', 'RUNNING')

Notice the conditions (WBDiscoRunning = Running/off, CouchShtBrk = Clear/break, StockPumpRunning = Running/off) change over the one hour.

The following query:

Select
MIN (RecordDate_Time) as [StartTime],
MAX (RecordDate_Time) as StopTime,
(DateDiff (ss, MIN (RecordDate_Time), MAX (RecordDate_Time))/60.0) as TotalTimeMIN
From tbl_1PMProduction
Where (RecordDate_Time >= '5/9/2005 10:34:00 AM' AND RecordDate_Time <= '5/9/2005 11:34:00 AM')
AND (WBDiscoRunning = 'Running' AND CouchShtBrk = 'Clear' AND StockPumpRunning
= 'Running')
Order by MIN (RecordDate_Time) DESC

Produces the following results:

StartTime StopTime TotalTimeMIN
2005-05-09 10:34:52.400 2005-05-09 11:29:52.400 55.000000

What I need for a result set is as follows:

StartTime StopTime TotalTimeMIN
2005-05-09 10:34:52.400 2005-05-09 10:38:52.400 4.000000
2005-05-09 11:05:52.400 2005-05-09 11:29:52.400 24.000000

Basically I am looking for an accumulation of time based a set of conditions:
WBDiscoRunning = 'Running' AND CouchShtBrk = 'Clear' AND StockPumpRunning = 'Running

I think I need to create a sub query to break the times down but am not sure how to do that. Any help you can provide is greatly appreciated.

Thanks.



Your limits are only as far as you set your boundries....

X002548
Not Just a Number

15586 Posts

Posted - 2005-05-12 : 09:29:18
Well, now that's how to post....

I'll give it a shot....

This though

quote:

Basically I am looking for an accumulation of time based a set of conditions:
WBDiscoRunning = 'Running' AND CouchShtBrk = 'Clear' AND StockPumpRunning = 'Running



Is not the whole story...your result set looks to idetify a break in the "process", and that parts not clear to me....damn hangover....



Brett

8-)
Go to Top of Page

jpiscit1
Posting Yak Master

130 Posts

Posted - 2005-05-12 : 10:37:46
The "desired" result set is identifying a break in the process. When the condition
(WBDiscoRunning = 'Running' AND CouchShtBrk = 'Clear' AND StockPumpRunning = 'Running) is NOT true, I am not interested in calculating that in the accumulated time. That is when the process is not saving stock and not making money. We are trying to see how much time is associated to making money. To have it itemized:

StartTime StopTime TotalTimeMIN
2005-05-09 10:34:52.400 2005-05-09 10:38:52.400 4.000000
2005-05-09 11:05:52.400 2005-05-09 11:29:52.400 24.000000

Provides a break down of the times related to profit.






Your limits are only as far as you set your boundries....
Go to Top of Page

Seventhnight
Master Smack Fu Yak Hacker

2878 Posts

Posted - 2005-05-12 : 11:09:58
This should do it.


Select
StartTime = min(StartTime),
EndTime = max(StartTime),
TotalTimeMin = datediff(n,min(StartTime),max(StartTime))
From
(
Select StartTime =A.RecordDate_Time, EndTime = min(B.RecordDate_Time)
From
(
Select *
From #tbl_1PMProduction
Where StockPumpRunning='RUNNING'
and CouchShtBrk='CLEAR'
and WBDiscoRunning='RUNNING'
) A
Left Join
(
Select *
From #tbl_1PMProduction
Where not (StockPumpRunning='RUNNING'
and CouchShtBrk='CLEAR'
and WBDiscoRunning='RUNNING')
) B
On A.RecordDate_Time < B.RecordDate_Time
Group By A.RecordDate_Time
) Z
Group By EndTime
Order By StartTime, EndTime


Corey

Secret Service Agent: Mr. President, you're urinating on me.
President Lyndon Johnson: I know I am. It's my prerogative.
Go to Top of Page

jpiscit1
Posting Yak Master

130 Posts

Posted - 2005-05-12 : 16:22:16
Excellent! Thanks for your help. Thats what I was looking for.

Your limits are only as far as you set your boundries....
Go to Top of Page
   

- Advertisement -