Solved

Query boolean event and duration of each instance

Posted on 2004-09-13
10
437 Views
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,
CK
0
Comment
Question by:Oh-Jay
10 Comments
 
LVL 15

Expert Comment

by:jdlambert1
Comment Utility
Can you post the names and descriptions of the columns?
0
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
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)
0
 
LVL 17

Expert Comment

by:BillAn1
Comment Utility
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)

0
 

Author Comment

by:Oh-Jay
Comment Utility
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.
0
 
LVL 17

Expert Comment

by:BillAn1
Comment Utility
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)

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Oh-Jay
Comment Utility
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

query-
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)
0
 
LVL 17

Expert Comment

by:BillAn1
Comment Utility
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?
0
 

Author Comment

by:Oh-Jay
Comment Utility
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
0
 
LVL 17

Accepted Solution

by:
BillAn1 earned 200 total points
Comment Utility
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

SELECT....
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 !!
0
 

Author Comment

by:Oh-Jay
Comment Utility
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.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Viewers will learn how the fundamental information of how to create a table.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now