当前位置:网站首页>postgresql 使用存储过程,拼接多张表,查询数据
postgresql 使用存储过程,拼接多张表,查询数据
2022-06-12 09:56:00 【自己的九又四分之三站台】
在使用过程中,由于数据随年份的变化数据会发生变化 ,现在需要每次统计时将数据汇总统计。
表名结构如下:
- unionall_table_2021
- unionall_table_2022
- unionall_table_2020
1. 将每年的数据显示union all拼接
create view vw_unionall_table
as
select * from unionall_table_2020
union all
select * from unionall_table_2021
union all
select * from unionall_table_2022
2. 使用存储过程,动态构建查询
CREATE OR REPLACE FUNCTION get_unionall_table()
RETURNS TABLE("objectid" int4, "f_tbbh" varchar, "f_dlbm" varchar, "f_area" numeric, "f_xzqdm" varchar, "f_year" int4) AS $BODY$
DECLARE
sqlSearch TEXT DEFAULT '';
rec_data RECORD;
cur_data REFCURSOR;
cur_tablename TEXT;
BEGIN-- Open the cursor
OPEN cur_data FOR SELECT
tablename
FROM
pg_tables
WHERE
tablename LIKE'unionall_table_%'
AND LENGTH ( tablename ) = LENGTH ( 'unionall_table_xxxx' );
FETCH cur_data INTO rec_data;
while
FOUND
LOOP-- fetch row into the film
cur_tablename := rec_data.tablename;
FETCH NEXT
FROM
cur_data INTO rec_data;
sqlSearch := sqlSearch || ' select objectid, f_tbbh,f_dlbm,f_area,f_xzqdm,f_year from ' || cur_tablename || ' union all ';
END LOOP;
-- Close the cursor
CLOSE cur_data;
sqlSearch := substr( sqlSearch, 1, "length" ( sqlSearch ) - LENGTH ( 'union all ' ) );
RETURN query EXECUTE sqlSearch;
END; $BODY$
LANGUAGE plpgsql VOLATILE
COST 100
ROWS 1000
create view vw_unionall_table
as
select get_unionall_table();
边栏推荐
- 日本经济泡沫与房价泡沫
- Auto.js学习笔记9:脚本引擎使用,启动指定路径脚本文件和关闭等基础方法
- 004:AWS数据湖解决方案
- Tap series article 3 | introduction to Tanzu application platform deployment reference architecture
- MYSQL的最左匹配原则的原理讲解
- 7-4 network red dot punch in strategy (DFS)
- 《第五项修炼》读书笔记
- MYSQL的最左匹配原則的原理講解
- Auto. JS learning notes 8: some common and important APIs
- June training (day 12) - linked list
猜你喜欢

2026年中国软件定义存储市场容量将接近45.1亿美元

一文读懂Dfinity生态中的首个NFT平台:IMPOSSIBLE THINGS

【clickhouse专栏】基础数据类型说明

《保护我们的数字遗产:DNA数据存储》白皮书发布

Papaya Mobile has a comprehensive layout of cross-border e-commerce SaaS papaya orange. What are the opportunities for this new track?

Autojs学习笔记6:text(txt).findOne()切换app时会报错,最后解决实现效果,切换任何app直到脚本找到指定的txt文字的控件进行点击。

Explication du principe d'appariement le plus à gauche de MySQL
![[preview of the open class of Jishu] arm's strongest MCU core cortex-m85 processor helps the innovation of the Internet of things in an all-round way (there is a lottery)](/img/25/c3af3f51c04865820e3bbe2f010098.png)
[preview of the open class of Jishu] arm's strongest MCU core cortex-m85 processor helps the innovation of the Internet of things in an all-round way (there is a lottery)

【云原生 | Kubernetes篇】Kubernetes 网络策略(NetworkPolicy)

哈希表的理论讲解
随机推荐
哈希表的线性探测法代码实现
日本经济泡沫与房价泡沫
Implementation of hotspot synchronized
[preview of the open class of Jishu] arm's strongest MCU core cortex-m85 processor helps the innovation of the Internet of things in an all-round way (there is a lottery)
High quality and good books help guide apes and recommend "good summer books" with the four major publishers
MySQL优化之慢日志查询
小程序介绍
Auto.js学习笔记7:js文件调用另一个js文件里的函数和变量,解决调用失败的各种问题
在线电路仿真以及开源电子硬件设计介绍
Auto. JS learning notes 7: JS file calls functions and variables in another JS file to solve various problems of call failure
价值投资.
Code implementation of hash table based on linear detection
MySQL optimized slow log query
【clickhouse专栏】基础数据类型说明
SAP Hana error message sys_ XSA authentication failed SQLSTATE - 28000
Explanation of the principle of MySQL's leftmost matching principle
小程序的介绍
六月集训(第12天) —— 链表
端午節安康--諸佬在我心裏越來越粽要了
TAP 系列文章3 | Tanzu Application Platform 部署参考架构介绍