我有一个函数,该函数带有两个声明为year
和month
的参数,如下所示:
CREATE OR REPLACE FUNCTION my_function(
_year integer,
_month integer)
RETURNS TABLE (
column_1 integer,
column_2 text
) AS
$$
BEGIN
-- Do somenthing here and return the data...
END
$$
LANGUAGE plpgsql;
我知道,如果我想获得函数调用的结果作为单独的列,则必须这样调用它:
SELECT * FROM my_function(2018, 7);
但是,我想打一个更复杂的电话,像这样:
SELECT my_function(year, month)
FROM (
SELECT
EXTRACT(YEAR FROM generate_series('2017-01-01', now(), interval '1 month'))::integer AS year,
EXTRACT(MONTH FROM generate_series('2017-01-01', now(), interval '1 month'))::integer AS month
) sub;
这将导致一组record
类型的行。指定这样的参数时如何获得“适当的”表行格式?
答案 0 :(得分:3)
SELECT j.year, j.month FROM
(
SELECT
EXTRACT(YEAR FROM generate_series('2017-01-01', now(), interval '1 month'))::integer AS year,
EXTRACT(MONTH FROM generate_series('2017-01-01', now(), interval '1 month'))::integer AS month
) t
JOIN LATERAL my_function(year, month) j(year, month)
on true