[Webinar] Streamline your web hosting managementRegister Today


Query boolean event and duration of each instance

Posted on 2004-09-13
Medium Priority
Last Modified: 2008-03-10
I have a historical logging server (Intellution iHistorian) that will allow me to retrieve my historical SCADA data and I am having trouble with an appropriate query (new to SQL).  

I am logging the open and closing of automated valves, around 40 of them to be exact, and I need to extract when they opened and how long they were open.  I assume I should be able to just get this from the open timestamp then do a calc with the close timestamp, just not sure of how to go about it.  The valves would likely cycle a few times a day and I would need to report on each cycle, really not sure about that part.  

Any help would be more than appreciated,
Question by:Oh-Jay
LVL 15

Expert Comment

ID: 12047269
Can you post the names and descriptions of the columns?
LVL 32

Expert Comment

by:Brendt Hess
ID: 12047606
A couple of questions:

(1) Is the data actually using a Timestamp SQL Server data type, or is it using Datetime?

(2) Is it possible that your data includes date transitions, e.g. open 30 seconds before midnight, close 45 second after midnight?

This form of query should do what you need...

Select Open.ValveID, Open.LogDateTime as OpenTime, DateDiff(ms,Open.LogDateTime, Close.LogDateTime ) / 1000.0 As OpenSeconds
FROM Logdata Open
INNER Join LogData Close ON Open.ValveID = Close.ValveID
And Open.State = 'OPEN'
And Close.LogDateTime =
              (Select Min(LogDateTime)
               From Logdata L
               WHERE L.ValveID = Open.ValveID
                    AND L.State = 'CLOSE'
                    And L. LogDateTime >= Open.LogDateTime)
LVL 17

Expert Comment

ID: 12047623
If your rows consist of valveID, TimeStamp and status (ON or OFF) then the following query will give you records with the on & off times, plu the durtation on for each.

select OnData.valveID, OnTime, OffTime, datediff(ss, OnTime, OffTime) as onDuration from
(select valveID, TimeStamp as OnTime from MyLog
where state = 'On' ) OnData
left outer join
(select valveID, TimeStamp as OffTime from MyLog
where state = 'Off') OffData
on OnData.valveID = OffData.valveID
and OffData.OffTime = (Select min(TimeStamp) from MyLog where valveID = OnData.valveID and TimeStamp > OnTime)

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions


Author Comment

ID: 12048571
Sorry about that, kinda left out some info.

this is the table that hold the actual data for the SCADA tags being collected.

 ihRawData Table
Column Name                 DataType                  Description
Tagname                        VT_BSTR                 Tagname property of the tag.
TimeStamp                     VT_DBTimeStamp     The date and time for the data sample.
Value                              VT_VARIANT            The value of the data.

to answer bhess1 2nd question it is likely that it would include date transitions.
LVL 17

Expert Comment

ID: 12049389
OK, then it is the same structure, only different column names , you can use this query :

depends still on what your table is actually called, and what values are wirttten when the valve is open/closed / on/off whatever.

select OnData.TagName, OnTime, OffTime, datediff(ss, OnTime, OffTime) as onDuration from
(select TagName, TimeStamp as OnTime from MyLog
where Value = 'On' ) OnData
left outer join
(select TagName, TimeStamp as OffTime from MyLog
where Value = 'Off') OffData
on OnData.TagName = OffData.TagName
and OffData.OffTime = (Select min(TimeStamp) from MyLog where TagName = OnData.TagName and TimeStamp > OnTime)


Author Comment

ID: 12056322
Sorry to be such a noob but still having trouble, I feel close thou. TIA

I replaced MyLog with ihRawData (table name) and used the set command to limit the date/time and the sample type (this maybe a little propriety but the system logs each tags collected in a few different modes). I get a table does not exist error that I haven't been able to figure .  tags would be like (valve1, valve2, ... with values available as either 0/1 or Open/Close) so the table contents would be like below for example --
TESTVALVE1.F_CV      9/13/2004 08:42:22      OPEN
TESTVALVE1.F_CV      9/13/2004 08:42:25      CLOSE
TESTVALVE3.F_CV      9/13/2004 08:42:26      OPEN
TESTVALVE2.F_CV      9/13/2004 08:42:26      OPEN
TESTVALVE4.F_CV      9/13/2004 08:42:32      OPEN
TESTVALVE1.F_CV      9/13/2004 08:42:34      OPEN
TESTVALVE2.F_CV      9/13/2004 08:42:34      CLOSE
TESTVALVE1.F_CV      9/13/2004 08:42:36      CLOSE
TESTVALVE4.F_CV      9/13/2004 08:42:36      CLOSE
TESTVALVE3.F_CV      9/13/2004 08:42:41      CLOSE
TESTVALVE2.F_CV      9/13/2004 08:42:42      OPEN

set samplingmode=rawbytime,starttime=yesterday, endtime=today

select OnData.TagName, OnTime, OffTime, datediff(ss, OnTime, OffTime) as onDuration from
(select TagName, TimeStamp as OnTime from ihRawData
where Value = 'Open' ) OnData
left outer join
(select TagName, TimeStamp as OffTime from ihRawData
where Value = 'Close') OffData
on OnData.TagName = OffData.TagName
and OffData.OffTime = (Select min(TimeStamp) from ihRawData where TagName = OnData.TagName and TimeStamp > OnTime)
LVL 17

Expert Comment

ID: 12056558
can you give the exact error you are getting?

Also, can you explain the line "set samplingmode=rawbytime,starttime=yesterday, endtime=today"
that is not SQL - what do you expect it to do?

Author Comment

ID: 12056931
query error: table does not exist

all this is slightly proprietary maybe part of the statement isn't supported but i have tried the main pieces and it works .

i some fields in the table one being the mode of sample raw just being the straight value collect at the timestamp, there is other samples taken automatically by the system i guess this set command will ignore the other sample types and limit the data fetched to the starttime/endtime (equates to using a where starttime > x and starttime < y)  

this maybe just a little too off normal SQL to work right I will ask in the app specific forums, they are just much smaller and hence slower than here, Thanks CK
LVL 17

Accepted Solution

BillAn1 earned 600 total points
ID: 12057123
OK, sorry I didn't twig you were using Intellution iHistorian to query the data. I thought it was just the system for collecting the data originally.
Do you know what database platform they use? Is it proproetary or do they use SQLServer/Oracle?

My guess is that if their application layer lets you use commands like "set samplingmode=rawbytime,starttime=yesterday, endtime=today"
it will be quite restrictive in therms of the SQL you can build. You are probably limited to a straightforward

FROM .......

i.e. no nested queries, etc.

I think you will need a more integrated solution, rather than being able to do it with straight SQL, sorry !!

Author Comment

ID: 12057283
it's proprietary unlike the other industrial packages doing similar data collection, i just found out that they don't support nesting select statements..thanks for the try got me close it was worth the points either way.

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.
Suggested Courses

607 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