PostgreSQL:将表值函数连续应用于一组值和UNION ALL结果 [英] PostgreSQL: Serially apply table-valued function to a set of values and UNION ALL results

查看:142
本文介绍了PostgreSQL:将表值函数连续应用于一组值和UNION ALL结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表值PL / pgsql函数,它作为1输入一个整数,一个ID。返回的表具有固定列(例如5),但行数不同。

I have a table-valued PL/pgsql function that takes as 1 input an integer, an ID. The table that is returned has fixed columns (say 5) but varying number of rows.

有一个包含这些唯一ID的大表格。我想将此函数应用于每个ID和UNION的所有结果。

There is a large table of these unique IDs. I'd like to apply this function to each ID and UNION ALL the results.

查看在线我一直看到CROSS APPLY作为解决方案,但它似乎在PostgreSQL中不可用。我如何做这个应用操作?

Looking online I keep seeing CROSS APPLY as the solution, but it does not appear to be available in PostgreSQL. How can I do this "apply" operation?

一个简单的解决方案是用额外的外部循环重写表值函数。

One trivial solution is to re-write the table-valued function with an additional outer loop. But is there a way to do this directly in SQL?

推荐答案

我认为在当前版本的PostgreSQL中是不可能做到的)。在9.3中,会有 LATERAL加入,这完全符合您的需要。

I think it's impossible to do in current version of PostgreSQL (9.2). In 9.3 there would be LATERAL join which does exactly what you want.

但是, apply 函数返回一组简单的值:

You can, however, apply function returning set of simple values:

select id, func(id) as f from tbl1

sql fiddle demo

这篇关于PostgreSQL:将表值函数连续应用于一组值和UNION ALL结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆