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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,835评论 4 364
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,598评论 1 295
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,569评论 0 244
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,159评论 0 213
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,533评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,710评论 1 222
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,923评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,674评论 0 203
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,421评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,622评论 2 245
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,115评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,428评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,114评论 3 238
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,097评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,875评论 0 197
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,753评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,649评论 2 271

推荐阅读更多精彩内容