Some useful SQL for OLEDB
Get snapshot for all tags
Get data for a digital tag
FROM piarchive..picomp2
WHERE tag = 'cdm158' AND time >= 't'
Get data for a tag list
WHERE tag IN (SELECT tag FROM pipoint..classic WHERE pointsource = 'R') AND time BETWEEN 'y' AND 't'
Get data for a tag list, with handling of digital states and bad statuses
Note: the usage of the CASE WHEN..THEN..ELSE..END conditional statement, which act exactly like an IF..THEN..ELSE statement, but in SQL queries
Get annotated events for a tag
WHERE tag = 'sinusoid' AND time >= '*-1mo' AND annotated = TRUE
Write a new snapshot value for a digital tag
VALUES ('cdm158', DIGCODE('Cascade', 'Modes'))
Write an archive value in the past
VALUES ('sinusoid', '*-1h', 5000)
Write an archive value with an annotation and the questionable flag
VALUES ('sinusoid', 'y+8h', 5000, 'Reason for special value…', TRUE)
Multiply values of a tag by 2
WHERE tag = 'sinusoid' AND time BETWEEN '*-2.5h' AND '*-2h'
Delete values for a given tag
WHERE tag = 'cdt158' AND time BETWEEN '*-8h' AND '*-7h
Get synchronized averages for 2 tags
INNER JOIN piarchive..piavg AS a2 ON a1.time = a2.time AND a1.timestep = a2.timestep
WHERE a1.time BETWEEN 'y' AND 't' AND a1.timestep = '1h'
AND a1.tag = 'sinusoid' AND a2.tag = 'sinusoidu'
Get Performance Equation tags (PointSource = C) whose snapshot value is:
WHERE tag IN (SELECT tag FROM pipoint..classic WHERE pointsource = 'C')
AND ((DATE('*') - time) BETWEEN RELDATE('4h') AND RELDATE('365d')
OR status <> 0)
Get List of tags
WHERE pointsource = 'R'
Count tags by PointSource attribute
FROM pipoint..classic GROUP BY pointsource
Create a tag
VALUES ('TestTag', 'float32')
Rename a tag
WHERE tag = 'TestTag'
Delete a tag
WHERE tag = 'TestTag2'
List all digital state sets
Create a Digital State Set
Create States in a Digital State Set
List all states in a specific digital state set
Rename a Digital State
WHERE name = 'TestState1'
Delete a Digital State Set
Get Sub-Modules of a specific Module
WHERE path = '\SomeTopModule\'
Get Modules under a specific Branch
WHERE path LIKE '\-%OSI\%' ESCAPE '-'
Use the ESCAPE statement to specify the first % character is textual while the second one corresponds to the wildcard character
Get Module Aliases
The server and tag columns should be looked at together, since aliases can come from different PI Servers
Get Module Properties
WHERE moduleuid = MODULEUID('\Some Module\SubModule)
The path column should be considered, since properties can be hierarchic
Create a Module at root of ModuleDB
VALUES ('TestModule')
Create a Sub-Module in a Module
VALUES ('TestSubModule', '\TestModule\')
Mark a Module as Unit (for UnitBatch support)
WHERE uid = MODULEUID('\TestModule')
Create a Module Alias
VALUES ( MODULEUID('\TestModule\TestSubModule'), MODULEEFFECTIVE(MODULEUID('\TestModule\TestSubModule')), 'TestAlias', 'sinusoid')
Create a Module Property
VALUES ( MODULEUID('\TestModule\TestSubModule'), MODULEEFFECTIVE(MODULEUID('\TestModule\TestSubModule')), 'TestProperty', 11)
There is no type column in the “piproperty” table. Use the CAST(Value AS Type) to explicitly specify the type
Move a Module in the Hierarchy
SET path = '\%OSI\' WHERE path = '\TestModule\' AND name = 'TestSubModule'
Permanently Delete a Module
WHERE uid = MODULEUID('\%OSI\TestSubModule')
Get Unit Batches Made in a Unit
WHERE moduleuid = MODULEUID('\Some Company\Some Division\Units A\Unit A1')
AND starttime >= 't' ORDER BY starttime
Get Batches with its UnitBatches and SubBatches
FROM pibatch..pibatch b INNER JOIN pibatch..piunitbatch u ON u.batchuid = b.uid
WHERE b.starttime > '*-1d' AND b.batchid = 'TestBatch' ORDER BY Batch, UBStart
Create a Unit Batch, linked to a Batch
VALUES ('TestUnitBatch', '*', MODULEUID('\TestModule'), (SELECT uid FROM pibatch..pibatch
WHERE batchid = 'TestBatch' AND starttime >= 't'))
End a Unit Batch
'WHERE batchid = 'TestUnitBatch' AND starttime >= 't' AND moduleuid = MODULEUID('\TestModule')










