当前位置:网站首页>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();
边栏推荐
- 《第五项修炼》读书笔记
- [path of system analyst] Chapter 18 security analysis and design of double disk system
- SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000
- Auto. JS learning note 9: basic methods such as using the script engine, starting the script file with the specified path, and closing
- In 2026, the capacity of China's software defined storage market will be close to US $4.51 billion
- 2022 pole technology communication - the dispute over anmou technology is settled, and the cornerstone of the local semiconductor industry is more stable
- Implementation of hotspot reference
- Autojs learning notes 6:text (txt) Findone() will report an error when switching apps. Finally, solve the implementation effect and switch any app until the script finds the control with the specified
- 奇葩错误 -- 轮廓检测检测到边框、膨胀腐蚀开闭运算效果颠倒
- [cloud native] what exactly does it mean? This article shares the answer with you
猜你喜欢

科创人·神州数码集团CIO沈旸:最佳实践模式正在失灵,开源加速分布式创新

FPGA基于DE2-115平台的VGA显示

GNU EFI development environment settings

What are the functions of resistance? (super full)

Ceph性能优化与增强

Is it necessary to separate databases and tables for MySQL single table data of 5million?

I Regular expression to finite state automata: regular expression to NFA

SAP HANA 错误消息 SYS_XSA authentication failed SQLSTATE - 28000

电脑启动快捷键一览表

UE4_以现成资源探索创建背景场景的方法
随机推荐
Jetpack架构组件学习(3)——Activity Results API使用
7-5 zhe zhe playing games
Papaya Mobile: cross border marketing has entered the era of "information flow", allowing independent station sellers to share opportunities to go to sea
Dragon Boat Festival Ankang - - les Yankees dans mon cœur de plus en plus de zongzi
使用Visual Studio 2017创建简单的窗口程序
Papaya Mobile has a comprehensive layout of cross-border e-commerce SaaS papaya orange. What are the opportunities for this new track?
在线电路仿真以及开源电子硬件设计介绍
The white paper "protecting our digital heritage: DNA data storage" was released
Tap series article 3 | introduction to Tanzu application platform deployment reference architecture
markdown_ Picture side by side scheme
002: what are the characteristics of the data lake
C# break continue return 三者区别
PandoraBox 使用防火墙规则定义非上网时间
Pandorabox uses firewall rules to define non internet time
硬盘 SMART 检测参数详解
GNU EFI development environment settings
Auto. JS learning note 4: after autojs is packaged, most Huawei and other big brand mobile phones cannot be installed? This problem can be solved by using the simulator to remotely sign and package in
Implementation of hotspot synchronized
科创人·世界500强集团CIO李洋:数字化转型成事在人,决策者应时刻聚焦于「柴」
How CEPH improves storage performance and storage stability