我有一個時間刻度資料庫,其中包含多個具有相同結構的表。
我想從每個值為真的表中檢索最近的行。
我的邏輯是
- 檢索條件可以為真的表的所有表名
- 回圈遍歷表名串列并選擇滿足條件的行
我在 FOR 回圈中遇到語法錯誤,但我希望我做錯更多的事情。
有人可以提出解決方案嗎?先感謝您。
DECLARE
tablename text;
BEGIN
FOR tablename IN
SELECT table_name FROM information_schema.tables
WHERE table_name LIKE 'ohlc%'
LOOP
SELECT WHERE tablename.is_active is TRUE
ORDER BY time_stamp DESC
Limit 1
END LOOP;
END;
uj5u.com熱心網友回復:
翻譯你的問題
- 在架構中查找具有特定列名的表。 如何在postgresql中查找具有特定列的表
- 第一個條件滿足然后回圈。 回圈并從多個表中選擇資料的函式
- 最棘手的問題是quote_ident。
create or replace function test0()
returns table (_is_active boolean, id int) as
$$
declare tbl text;
begin
for tbl in
select quote_ident( table_name)
from information_schema.columns
where table_schema = 'public'
and table_name ilike 'ohlc%'
and column_name = 'is_active'
loop
return query EXECUTE
'select ' || quote_ident('is_active') || ' , ' || quote_ident('id') || ' from ' || tbl || ' where '|| quote_ident('is_active') ||' is true';
end loop;
end
$$ language plpgsql;
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/439855.html
標籤:sql PostgreSQL 时间尺度数据库
上一篇:我可以從哪里開始創建用于管理作業串列的RESTAPI?
下一篇:在兩行中寫入兩個非Null值
