Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

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)

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.


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

Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

718 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