pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
Returning a table from a function, that requires multiple selects?
6+ messages / 4 participants
[nested] [flat]

* Returning a table from a function, that requires multiple selects?
@ 2019-08-30 20:03  stan <stanb@panix.com>
  0 siblings, 3 replies; 6+ messages in thread

From: stan @ 2019-08-30 20:03 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Returning a table from a function, that requires multiple selects?
@ 2019-08-30 20:09  Rob Sargent <robjsargent@gmail.com>
  parent: stan <stanb@panix.com>
  2 siblings, 0 replies; 6+ messages in thread

From: Rob Sargent @ 2019-08-30 20:09 UTC (permalink / raw)
  To: stan <stanb@panix.com>; +Cc: pgsql-general@lists.postgresql.org



> 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.

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Returning a table from a function, that requires multiple selects?
@ 2019-08-30 20:09  Guyren Howe <guyren@gmail.com>
  parent: stan <stanb@panix.com>
  2 siblings, 1 reply; 6+ messages in thread

From: Guyren Howe @ 2019-08-30 20:09 UTC (permalink / raw)
  To: stan <stanb@panix.com>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

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.=

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Returning a table from a function, that requires multiple selects?
@ 2019-08-30 20:23  Rob Sargent <robjsargent@gmail.com>
  parent: Guyren Howe <guyren@gmail.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Rob Sargent @ 2019-08-30 20:23 UTC (permalink / raw)
  To: Guyren Howe <guyren@gmail.com>; +Cc: stan <stanb@panix.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>



> 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.=

^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Returning a table from a function, that requires multiple selects?
@ 2019-08-30 20:34  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Rob Sargent <robjsargent@gmail.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Tom Lane @ 2019-08-30 20:34 UTC (permalink / raw)
  To: Rob Sargent <robjsargent@gmail.com>; +Cc: Guyren Howe <guyren@gmail.com>; stan <stanb@panix.com>; pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>

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





^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: Returning a table from a function, that requires multiple selects?
@ 2019-08-30 22:12  stan <stanb@panix.com>
  parent: stan <stanb@panix.com>
  2 siblings, 0 replies; 6+ messages in thread

From: stan @ 2019-08-30 22:12 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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





^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2019-08-30 22:12 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-08-30 20:03 Returning a table from a function, that requires multiple selects? stan <stanb@panix.com>
2019-08-30 20:09 ` Rob Sargent <robjsargent@gmail.com>
2019-08-30 20:09 ` Guyren Howe <guyren@gmail.com>
2019-08-30 20:23   ` Rob Sargent <robjsargent@gmail.com>
2019-08-30 20:34     ` Tom Lane <tgl@sss.pgh.pa.us>
2019-08-30 22:12 ` stan <stanb@panix.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox