Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 454
  • Last Modified:

Query boolean event and duration of each instance

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
Oh-Jay
Asked:
Oh-Jay
1 Solution
 
jdlambert1Commented:
Can you post the names and descriptions of the columns?
0
 
Brendt HessSenior DBACommented:
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
 
BillAn1Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Oh-JayAuthor Commented:
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
 
BillAn1Commented:
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
 
Oh-JayAuthor Commented:
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
 
BillAn1Commented:
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
 
Oh-JayAuthor Commented:
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
 
BillAn1Commented:
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
 
Oh-JayAuthor Commented:
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now