Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call a function for each row in select - Postgres

Tags:

I have a function called "getList(date)". This function returns me a items list (with several columns) from the date inputted in the parameter.

If I call:

SELECT * FROM getList('12/31/2014'); 

It works fine. It returns me a list with the date, the item name and the price.

Something like this:

date        item_description    price ----------------------------------------------- 12/31/2014      banana          1 12/31/2014      apple           2.5 12/31/2014      coconut         3 

But I have another table with the dates that I want to search for.

So, I want to select all the dates from that table and, for each row returned, I want to call my function "getList" to have a result like this:

 date       item_description    price  -----------------------------------------------  12/28/2014     banana          0.5  12/28/2014     apple           1.5  12/28/2014     coconut         2  12/31/2014     banana          1  12/31/2014     apple           2.5  12/31/2014     coconut         3 

I don't know exactly how to do it. Of course my data is not a fruit list. This is just to explain the whole thing easier.

Thank you very much.

like image 551
Gilbert Avatar asked Jan 16 '15 19:01

Gilbert


People also ask

How do you call a function within a Postgres function?

The normal syntax to call another PL/pgSQL function from within PL/pgSQL is to either reference the function in a SQL SELECT statement, or during the assignment of a variable. For example: SELECT function_identifier ( arguments ); variable_identifier := function_identifier ( arguments );

How do I SELECT a row in PostgreSQL?

The heart of all SQL queries is the SELECT command. SELECT is used to build queries (also known as SELECT statements). Queries are the only SQL instructions by which your data can be retrieved from tables and views.

What is $$ in PostgreSQL function?

It can be used to replace single quotes enclosing string literals (constants) anywhere in SQL scripts. The body of a function happens to be such a string literal. Dollar-quoting is a PostgreSQL-specific substitute for single quotes to avoid escaping of nested single quotes (recursively).

How do you run a function with out parameters in PostgreSQL?

The OUT parameters are declared as a part of the argument list and are returned as a part of the result. The OUT parameters are very useful in functions that require returning multiple values. They act like uninitialized variables. Unlike the IN parameters, a value must be assigned to the OUT parameters.


1 Answers

Correct way - LATERAL join

The correct way to do this is with a lateral query (PostgreSQL 9.3 or newer):

SELECT d."date", f.item_description, f.price FROM mydates d,      LATERAL getList(d."date") f; 

See the manual.

Legacy way - SRF in SELECT

In older versions you must use a PostgreSQL extension with some ... quirky ... properties, support for set-returning functions in the SELECT-list. Do not use this unless you know you must support PostgreSQL 9.2 or older.

SELECT d."date", (getList(d."date").* FROM mydates d; 

This may result in multiple-evaluation of the getList function, once for each column of the output.

like image 55
Craig Ringer Avatar answered Oct 18 '22 13:10

Craig Ringer