PISQL

-- Archive Statements


-- Sample queries

-- Returns today's "cdm158" events translated into digital state names.
-- The "picomp" table is a PI SQL Subsystem compatible table representing
-- archive values using 3 columns: "value", "svalue", and "status".
SELECT tag, time, DIGSTRING(status) value
FROM piarchive..picomp WHERE tag = 'cdm158' AND time >= 't'

-- Returns today's "cdm158" events translated into digital state names.
-- The "picomp2" table represents values as VARIANTs. Thus, you might
-- need to cast the "value" column prior to using it as a function argument.
SELECT tag, time, DIGSTRING(CAST(value AS Int32)) value, DIGSTRING(status) status
FROM piarchive..picomp2 WHERE tag = 'cdm158' AND time >= 't'

-- Returns snapshots of all tags.
-- The "time = ''" WHERE condition in the "picomp" table has a special
-- meaning - returns snapshots although they are not archived at the current time.
SELECT tag, time, value, svalue, status FROM piarchive..picomp WHERE time = '
'

-- Returns snapshots of all tags.
-- As well as the "picomp2" table, the "pisnapshot" table represents
-- values as VARIANTs.
SELECT tag, time, value, status FROM piarchive..pisnapshot

-- Returns the last 10 events of the "sinusoid" tag.
-- Both the "picomp" and the "picomp2" table optimizes "SELECT TOP ... ORDER BY tag, time DESC"
-- statements so that you can quickly get the most recent events.
SELECT TOP 10 tag, time, value, status FROM piarchive..picomp2
WHERE tag = 'sinusoid' ORDER BY tag, time DESC

-- Returns the last 10 events of the "sinusoid" tag ordered chronologically in ascending order.
SELECT tag, time, value, status
FROM
(
SELECT TOP 10 tag, time, _index, value, status
FROM piarchive..picomp2
WHERE tag = 'sinusoid'
ORDER BY tag, time DESC
) t
ORDER BY tag, time, _index

-- Returns number of "sinusoid" events since the beginning of the current month.
-- PI OLEDB optimizes "count" queries into the "picomp" and "picomp2" tables so that you can
-- quickly get number of events in a time interval.
SELECT COUNT() FROM piarchive..picomp2 WHERE tag = 'sinusoid' AND time >= BOM('')

-- Returns "bad and stale" calculated tags.
-- Using a SQL WHERE condition, you can define any criterion for being "stale and bad".
-- In this case, the criterion is either snapshot older than 4 hours and newer than 1
-- year or snapshot with a bad status.
SELECT tag FROM piarchive..pisnapshot
WHERE tag IN (SELECT tag FROM pipoint..classic WHERE pointsource = 'C')
AND ((DATE('*') - time) BETWEEN RELDATE('4h') AND RELDATE('365d') OR status <> 0)

-- Creates an annotated "sinusoid" event.
-- Annotations are accessible only through the "picomp2" table.
-- The "picomp" table indicates annotated events using the "flags" column,
-- but you can neither modify nor retrieve the annotations data.
INSERT piarchive..picomp2 (tag, time, value, annotations)
VALUES ('sinusoid', 't+8h', 1.0, 'Manually created')

-- Returns today's annotated "sinusoid" events.
SELECT time, value, annotations
FROM piarchive..picomp2
WHERE tag = 'sinusoid' AND time >= 't' AND annotated = TRUE

-- Deletes a "sinusoid" event.
-- The "picomp2" table supports all DML statements, while the PI SQL Subsystem
-- compatible "picomp" table only allows you to create new events using INSERT statements.
DELETE piarchive..picomp2 WHERE tag = 'sinusoid' AND time = 't+8h'

-- Creates a "sinusoid" event with the "Questionable" flag.
INSERT piarchive..picomp (tag, time, value, flags) VALUES ('sinusoid', 't+8h', 1.0, 'Q')

-- Creates a "sinusoid" event with the "Questionable" flag.
INSERT piarchive..picomp2 (tag, time, value, questionable) VALUES ('sinusoid', 't+8h', 1.0, TRUE)

-- Deletes a "sinusoid" event.
DELETE piarchive..picomp2 WHERE tag = 'sinusoid' AND time = 't+8h'

-- Creates a digital tag event.
-- Note that the "picomp" table returns digital tag values in the "status" column.
INSERT piarchive..picomp (tag, time, status) VALUES ('cdm158', 't+8h', DIGCODE('Auto', 'Modes'))

-- Deletes a "cdm158" event.
DELETE piarchive..picomp2 WHERE tag = 'cdm158' AND time = 't+8h'

-- Creates a digital tag event.
-- Contrary to the "picomp" table", the "picomp2" table always returns values in the "value" column.
INSERT piarchive..picomp2 (tag, time, value) VALUES ('cdm158', 't+8h', DIGCODE('Auto', 'Modes'))

-- Deletes a "cdm158" event.
DELETE piarchive..picomp2 WHERE tag = 'cdm158' AND time = 't+8h'

-- Creates a "float32" demo tag.
INSERT pipoint..classic (tag, pointtypex) VALUES ('sinusoid2', 'float32')

-- Copies data from the "sinusoid" tag into the "sinusoid2" tag using the "picomp" table.
INSERT piarchive..picomp (tag, time, value, status, flags)
SELECT 'sinusoid2', time, value, status, flags
FROM piarchive..picomp
WHERE tag = 'sinusoid' AND time BETWEEN 't' AND '*'

-- Copies data from the "sinusoid" tag into the "sinusoid2" tag using the "picomp2" table.
-- If you also executed the previous statement, the "sinusoid2" tag has two events at the same
-- timestamp for all the copied events.
INSERT piarchive..picomp2 (tag, time, value, status, questionable, annotations)
SELECT 'sinusoid2', time, value, status, questionable, annotations
FROM piarchive..picomp2
WHERE tag = 'sinusoid' AND time BETWEEN 't' AND '*'

-- Deletes the demo tag.
DELETE pipoint..classic WHERE tag = 'sinusoid2'

-- Creates a "sinusoid" event. The event is positioned as the first value at "today 08:00". Any existing values are moved forward.
INSERT piarchive..picomp2 (tag, time, _index, value) VALUES ('sinusoid', 't+8h', 1, 2.0)

-- Deletes the first "sinusoid" value at "today 08:00".
-- When deleting a specific event at a timestamp, you can delete just one event
-- by one DELETE statement, i.e. you must restrict all primary key columns in the WHERE condition.
DELETE piarchive..picomp2 WHERE tag = 'sinusoid' AND time = 't+8h' AND _index = 1

-- Multiplies today's "sinusoid" events by factor 2.
UPDATE piarchive..picomp2
SET value = 2.0 * CAST(value AS Float32)
WHERE tag = 'sinusoid' AND time >= 't'

-- Deletes today's "sinusoid" events.
-- You should be very cautious when executing DELETE statements. We strongly recommend executing a
-- SELECT statement with the same WHERE condition first to verify data to be deleted.
DELETE piarchive..picomp2 WHERE tag = 'sinusoid' AND time >= 't'

-- Returns yesterday�s "sinusoid" interpolations with 1 hour time step.
-- The "piinterp" table is a PI SQL Subsystem compatible table representing
-- archive values using 3 columns: "value", "svalue", and "status".
SELECT tag, time, value, status
FROM piarchive..piinterp
WHERE tag = 'sinusoid' AND time BETWEEN 'y' AND 't' AND timestep = '1h'

-- Returns yesterday�s "sinusoid" interpolations with 1 hour time step.
-- The "piinterp2" table represents values as VARIANTs.
SELECT tag, time, value, status
FROM piarchive..piinterp2
WHERE tag = 'sinusoid' AND time BETWEEN 'y' AND 't' AND timestep = '1h'

-- Returns yesterday�s event-weighted "sinusoid" averages with 1 hour time step.
-- PI OLEDB 3 enhances PI SQL Subsystem summary tables ("piavg", "pimin", "pimax", etc.)
-- with features supported in PI SDK. For details about calculation bases and summary functions,
-- which are not supported by the PI SQL Subsystem, you can also refer to the PI SDK help.
SELECT tag, time, value FROM piarchive..piavg
WHERE tag = 'sinusoid' AND time BETWEEN 'y' AND 't'
AND timestep = '1h' AND calcbasis = 'EventWeighted'

-- Returns today�s "sinusoid" events with the corresponding "sinusoidu" interpolations.
-- The "picomp" table serves as a source of timestamps for the "piinterp" table. Potentially,
-- you can join the "picomp" table with unlimited numbers of "piinterp" table instances.
-- This way, you can retrieve synchronized data for unlimited number of points.
SELECT c.tag, c.time, c.value, c.status, i.tag, i.value, i.status
FROM piarchive..picomp2 c
INNER JOIN piarchive..piinterp2 i ON i.time = c.time
WHERE c.tag = 'sinusoid' AND c.time >= 't' AND i.tag = 'sinusoidu'

-- Returns yesterday�s "sinusoid" and "sinusoidu" interpolations with 1 hour time step.
-- The first instance of the "piinterp" table serves as a source of timestamps for the second one.
-- Potentially, you can extend the join with unlimited number of the "piinterp" table instances.
SELECT i1.tag, i1.time, i1.value, i1.status, i2.tag, i2.value, i2.status
FROM piarchive..piinterp i1
INNER JOIN piarchive..piinterp i2 ON i1.time = i2.time
WHERE i1.tag = 'sinusoid' AND i1.time BETWEEN 'y' AND 't'
AND i1.timestep = '1h'AND i2.tag = 'sinusoidu'

-- Returns yesterday's "sinusoid" and "sinusoidu" time-weighted averages with 1 hour step.
-- The first instance of the "piavg" table serves as a source of timestamps for the second one.
-- Potentially, you can extend the join with unlimited number of the "piavg" table instances.
SELECT a1.tag, a1.time, a1.value, a2.tag, a2.value
FROM piarchive..piavg a1
INNER JOIN piarchive..piavg a2 ON a1.time = a2.time
WHERE a1.tag = 'sinusoid' AND a1.time BETWEEN 'y' AND 't' AND a1.timestep = '1h'
AND a2.tag = 'sinusoidu' AND a2.timestep = '1h'

-- Returns today�s events for the "classic" points of point source "R".
SELECT tag, time, value, status FROM piarchive..picomp2
WHERE tag IN (SELECT tag FROM pipoint..classic WHERE pointsource = 'R') AND time >= 't'

-- Finds the nearest "sinusoid" event to the today's midnight.
-- The "... FROM (SELECT 'sinusoid' mytag, DATE('t') mytime) p ..." part of the
-- statement defines query parameters - tag and time. As you can see here,
-- SQL CASE operator allows you to incorporate logic into SQL queries so that
-- for simple algorithms, you can use SQL instead of programming.
SELECT CASE WHEN (mytime - prevtime) < (nexttime - mytime) THEN prevtime ELSE nexttime END
FROM
(
SELECT mytime,
(
SELECT TOP 1 time
FROM piarchive..picomp2
WHERE tag = p.mytag AND time <= p.mytime
ORDER BY tag, time DESC
) prevtime,
(
SELECT TOP 1 time
FROM piarchive..picomp2
WHERE tag = p.mytag AND time >= p.mytime
ORDER BY tag, time ASC
) nexttime
FROM
(
SELECT 'sinusoid' mytag, DATE('t') mytime
) p
) t

你可能感兴趣的:(PISQL)