Returning a table from a function, that requires multiple selects?

Started by stanover 6 years ago6 messagesgeneral
Jump to latest
#1stan
stanb@panix.com

I need to encapsulate, what are basically 2 related function calls into a single
function. The result of each of th calls is a date type.

y current thinking is to return a 2 row table with the 2 dates in it. But, I seem to
be having issues getting this to work.

Is it possible for a function to return a table with results from multiple
queries?

--
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
-- Benjamin Franklin

#2Rob Sargent
robjsargent@gmail.com
In reply to: stan (#1)
Re: Returning a table from a function, that requires multiple selects?

On Aug 30, 2019, at 2:03 PM, stan <stanb@panix.com> wrote:

I need to encapsulate, what are basically 2 related function calls into a single
function. The result of each of th calls is a date type.

y current thinking is to return a 2 row table with the 2 dates in it. But, I seem to
be having issues getting this to work.

Is it possible for a function to return a table with results from multiple
queries?

--
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
-- Benjamin Franklin

If the two queries are identical in returned columns you might be able to use UNION:
select f1.* from first_query as f1 UNION select f2.* from second_query as f2;

You can’t do any processing of f1 or f2.

#3Guyren Howe
guyren@gmail.com
In reply to: stan (#1)
Re: Returning a table from a function, that requires multiple selects?

On Aug 30, 2019, at 13:03 , stan <stanb@panix.com> wrote:

I need to encapsulate, what are basically 2 related function calls into a single
function. The result of each of th calls is a date type.

y current thinking is to return a 2 row table with the 2 dates in it. But, I seem to
be having issues getting this to work.

Is it possible for a function to return a table with results from multiple
queries?

You could just return a tuple VALUES(a, b). Or you could define a type to return if you want to get fancy.

#4Rob Sargent
robjsargent@gmail.com
In reply to: Guyren Howe (#3)
Re: Returning a table from a function, that requires multiple selects?

On Aug 30, 2019, at 2:09 PM, Guyren Howe <guyren@gmail.com> wrote:

On Aug 30, 2019, at 13:03 , stan <stanb@panix.com <mailto:stanb@panix.com>> wrote:

I need to encapsulate, what are basically 2 related function calls into a single
function. The result of each of th calls is a date type.

y current thinking is to return a 2 row table with the 2 dates in it. But, I seem to
be having issues getting this to work.

Is it possible for a function to return a table with results from multiple
queries?

You could just return a tuple VALUES(a, b). Or you could define a type to return if you want to get fancy.

Here I you might want VALUE(array[‘heading1’,a], array[‘heading2',b]) unless you’re certain you know which date is which.

#5Tom Lane
tgl@sss.pgh.pa.us
In reply to: Rob Sargent (#4)
Re: Returning a table from a function, that requires multiple selects?

Rob Sargent <robjsargent@gmail.com> writes:

On Aug 30, 2019, at 2:09 PM, Guyren Howe <guyren@gmail.com> wrote:

On Aug 30, 2019, at 13:03 , stan <stanb@panix.com <mailto:stanb@panix.com>> wrote:

Is it possible for a function to return a table with results from multiple
queries?

You could just return a tuple VALUES(a, b). Or you could define a type to return if you want to get fancy.

Here I you might want VALUE(array[‘heading1’,a], array[‘heading2',b]) unless you’re certain you know which date is which.

Yeah, that would be a good reason to return a declared composite type.
Something like

CREATE TYPE two_dates AS (start date, stop date);

CREATE FUNCTION f(...) RETURNS two_dates AS ...;

SELECT * FROM f(...);

regards, tom lane

#6stan
stanb@panix.com
In reply to: stan (#1)
Re: Returning a table from a function, that requires multiple selects?

On Fri, Aug 30, 2019 at 04:03:15PM -0400, stan wrote:

I need to encapsulate, what are basically 2 related function calls into a single
function. The result of each of th calls is a date type.

y current thinking is to return a 2 row table with the 2 dates in it. But, I seem to
be having issues getting this to work.

Is it possible for a function to return a table with results from multiple
queries?

Got it working, so yes this can be done.

Next I have to figure out how to configure the next function to accept this table.
Error message says something about configuring it to accept a record.
--
"They that would give up essential liberty for temporary safety deserve
neither liberty nor safety."
-- Benjamin Franklin