Lab 3 - Query Examples 3-31: Wonderware System Platform Course - Part 2
Lab 3 - Query Examples 3-31: Wonderware System Platform Course - Part 2
FROM History
WHERE Tagname = 'TT_001.PV'
AND Datetime > 'January 1, 1970 10:00'
AND Datetime <= Getdate()
-- Example 12
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- Example 13
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SELECT *
FROM OpenQuery (INSQL, '
SELECT Datetime, [LIT_001.PV], [InletValve_001.PV.IsPassive],
SysString
FROM WideHistory
WHERE Datetime > Dateadd(mi, -65, Getdate())
AND Datetime <= Getdate()')
-- In the results of the preceding query, notice that a value is given for
-- SysString even though it only changed at the top of the hour, When using
-- delta queries on a wide table, a new row is returned if any of the requested
-- tags change value.
-- Example 14
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- Process data users use data differently than most other users of databases
-- (banks, scientific communities etc.). To allow our users to view the data in
-- the most advantagious way, a number of special functions were added to the
-- Transact-SQL language. We call these functions RealTime SQL. The first we
-- will explore is the wwRetrievalMode function. wwRetrievalMode allows the user
-- to override the default retrieval mode for the history tables on a per query
-- basis.
-- First is the concept of "Cyclic" versus "Delta" retrieval modes. The History
-- table returns data using Delta retrieval mode as default. This means that a
-- new row is returned only if the value changes. Notice that the interval
-- between each row is different for the following queries:
Wonderware Training
Lab 3 – Query Examples 3-33
-- Cyclic retrieval is useful when looking for profiles and averages, while
-- Delta retrieval is useful when looking for alterations in data, like a
-- setpoint change that occured once during a shift.
-- Example 15
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- For full retrieval mode, all stored values will be returned, regardless
-- of whether a value or quality has changed since the last value. If used in
-- conjunction with storage without filtering (no delta or cyclic storage mode
-- applied), the full retrieval mode allows for the retrieval of all values and
-- quality that originated from the plant floor data source or from another
-- application.
-- Example 16
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- Using 'wwResolution'
Wonderware Training
Lab 3 – Query Examples 3-35
-- Notice that Historian returns 100 rows evenly spaced over the
-- time period specified. This is the default behaviour, but there are
-- mechanisms to specify 'how much' data you want.
-- Example 17
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SELECT *
FROM OpenQuery(InSQL, 'SELECT Datetime, [LIT_001.PV], [TT_001.PV],
[InletValve_001.PV.IsActive1]
FROM WideHistory
WHERE Datetime > Dateadd(hh, -1, Getdate())
AND Datetime <= Getdate()
AND wwRetrievalMode = "Cyclic"
AND wwResolution = 60000')
-- Example 18
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- Using 'wwCycleCount'
Wonderware Training
Lab 3 – Query Examples 3-37
FROM History
WHERE Tagname = 'SysTimeSec'
AND wwRetrievalMode = 'delta'
AND Datetime > Dateadd(mi, -10, Getdate())
AND Datetime <= Getdate()
AND wwCycleCount = 25
-- This is useful when trying to retrieve data when the sample rate
-- is unknown.
-- Example 19
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- To get an average calculation from history data, we need to decide how much
-- data goes into the query, allowing us to decide how much work the InSQL
-- Server computer performs. The following is a rough estimate of an hourly
-- average using one sample every minute:
SELECT AVG(Value)
FROM History
WHERE Tagname = 'TT_001.PV'
AND Datetime > Dateadd(hh, -1, Getdate())
AND Datetime <= Getdate()
-- The SQL Server AVG aggregate is a simple statistical average. The result will
-- depend on the resolution of samples. Using the time weighted average (TWA)
-- retrieval mode will use actual values in the table to calculate the average.
-- Even if the SQL Server AVG aggregate returns a similar value, the data is
-- returned much faster. The following example will return the average for the
-- last hour:
-- Example 20
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- Time Weighted Average (TWA) retrieval mode is a true cyclic mode, so you can
-- divide the entire period of time into several periods. The historian will
-- return one row for each tag in the query for each cycle. To specify how many
-- cycles you want, you can use wwResolution or wwCycleCount.
-- The following query will return an hourly time weighted average for the last
-- shift:
Wonderware Training
Lab 3 – Query Examples 3-39
-- Example 21
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- The time-in-state retrieval mode returns the amount of time that a tag has
-- been in a particular state for each retrieval cycle. This retrieval mode is
-- useful for determining how long a machine has been running or stopped, how
-- long a valve has been opened or closed, and so on:
-- Example 22
-- ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
-- For the "best fit" retrieval mode, the total time for the query is divided
-- into even sub-periods, and then up to five values are returned for each
-- sub-period: first, last, minimum, maximum and first 'exception' values. This
-- mode allows for a compromise between delta retrieval mode and cyclic
-- retrieval mode:
-- Observe the amount of values returned for the above query, compared to the
-- values returned for a delta retrieval:
Wonderware Training