1

I have a user defined function. This question shows how to loop through dates. Using this approach, I tried this query:

select myfun(a::date) from generate_series('2015-01-01'::date,'2016-01-27','1 day') s(a)

This doesn't quite work. What it returns is a single column of the form:

(10101, "Sample",  "test")
(10102, "Sample2", "test2")

When in reality there should be three columns. It merges them into one.

I noticed that this is the same behavior that you get in a vanilla query such as select mytable when I omit the asterisk. The above query doesn't have an asterisk in it, but adding one causes an error.

1 Answer 1

6

Place the function call in the FROM clause:

select f.*
from 
    generate_series('2015-01-01'::date,'2016-01-27','1 day') s(a),
    myfun(a::date) f;

or using more formal syntax:

select f.*
from generate_series('2015-01-01'::date,'2016-01-27','1 day') s(a)
cross join myfun(a::date) f;

This form of the FROM clause is known as lateral join.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.