user1450576
user1450576

Reputation: 21

Can I get each field in a returned TABLE inside a SELECT?

I have a function which returns a table.

ordersum(from_date DATE, limit_date DATE) RETURNS TABLE (antal BIGINT, kundkategori text)

In my SELECT I call the function:

SELECT start_date,end_date,
ordersum(first_day(start_date),last_day(end_date))
from table 

Now the result looks like this:

startdate,end_date,ordersum
2015-01-01,2016-01-01,(1,"single order")

Can I get each field in the table in my select result? I can not call the function twice as it is very heavy.

startdate, end_date,antal,customer_category
2015-01-01,2016-01-01,1,"single order"

Upvotes: 0

Views: 37

Answers (2)

Stefanov.sm
Stefanov.sm

Reputation: 13049

Use a lateral join:

SELECT 
    t.start_date startdate, t.end_date, 
    os.antal quantity, os.kundkategori ordersum
from _table t
cross join lateral ordersum(first_day(start_date),last_day(end_date)) os;

Upvotes: 1

user330315
user330315

Reputation:

A function that returns a table, should be used like a table:

SELECT start_date,
       end_date,
       os.*
from table 
  cross join ordersum(first_day(start_date),last_day(end_date)) as os

Upvotes: 1

Related Questions