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閱讀 229,001評論 6 537
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,786評論 3 423
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,986評論 0 381
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,204評論 1 315
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,964評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,354評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,410評論 3 444
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,554評論 0 289
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 49,106評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,918評論 3 356
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,093評論 1 371
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,648評論 5 362
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,342評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,755評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,009評論 1 289
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,839評論 3 395
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 48,107評論 2 375

推薦閱讀更多精彩內容