Query boolean event and duration of each instance

Posted on 2004-09-13
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 15

Expert Comment

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

Expert Comment

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)

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


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 200 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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Job Hung 17 36
Need sql in string 2 29
Specify timing interval fro change data 2 53
Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

756 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