set-returning-functions

How to cast entity to set in PostgreSQL

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-11 08:07:13
问题 Using Postgres 9.3, I found out that I can perform something like this: SELECT generate_series(1,10); But I can't do this: SELECT (SELECT generate_series(1,10)); Can I somehow cast SELECT result to setof int to use it same as result from generate_series() ? What exactly is happening there why I can use result from function but not from SELECT ? 回答1: Your first form is a non-standard feature of Postgres. It allows SRF (Set Returning Functions) in the SELECT list, which are expanded to multiple

Is it possible to answer queries on a view before fully materializing the view?

戏子无情 提交于 2019-12-11 07:16:43
问题 In short: Distinct,Min,Max on the Left hand side of a Left Join, should be answerable without doing the join. I’m using a SQL array type (on Postgres 9.3) to condense several rows of data in to a single row, and then a view to return the unnested normalized view. I do this to save on index costs, as well as to get Postgres to compress the data in the array. Things work pretty well, but some queries that could be answered without unnesting and materializing/exploding the view are quite

“ERROR: column … specified more than once” in VIEW definition

前提是你 提交于 2019-12-11 05:14:07
问题 This is a follow-up question to an earlier one. I have a stored function f1 that takes two arguments returns a table with 5 columns; for now the returned values are constant, later they will be calculated from the arguments. I also have a table t1 with two columns that correspond in type to f1 's arguments. I would now like to define a view v1 that contains the union of all rows returned from f1 for all argument pairs stored in t1 . For the given example values the result should be: +---+---+

How to get elements with a unique number from a json array in PostgreSQL?

和自甴很熟 提交于 2019-12-11 03:18:49
问题 I have a table bank_accounts : Column | Type | Modifiers | Storage | Stats target | Description ---------------+-----------------------+-------------------------------------------------------------------------+----------+--------------+------------- id | integer | not null default nextval('bank_accounts_id_seq'::regclass) | plain | | name | character varying(50) | | extended | | bank_accounts | jsonb | not null | extended | | And it has some JSON in the jsonb column: id | name | bank_accounts

Return column name and distinct values

喜你入骨 提交于 2019-12-11 01:15:03
问题 Say I have a simple table in postgres as the following: +--------+--------+----------+ | Car | Pet | Name | +--------+--------+----------+ | BMW | Dog | Sam | | Honda | Cat | Mary | | Toyota | Dog | Sam | | ... | ... | ... | I would like to run a sql query that could return the column name in the first column and unique values in the second column. For example: +--------+--------+ | Col | Vals | +--------+--------+ | Car | BMW | | Car | Toyota | | Car | Honda | | Pet | Dog | | Pet | Cat | |

Compare result of two table functions using one column from each

僤鯓⒐⒋嵵緔 提交于 2019-12-10 11:21:31
问题 According the instructions here I have created two functions that use EXECUTE FORMAT and return the same table of (int,smallint) . Sample definitions: CREATE OR REPLACE FUNCTION function1(IN _tbl regclass, IN _tbl2 regclass, IN field1 integer) RETURNS TABLE(id integer, dist smallint) CREATE OR REPLACE FUNCTION function2(IN _tbl regclass, IN _tbl2 regclass, IN field1 integer) RETURNS TABLE(id integer, dist smallint) Both functions return the exact same number of rows. Sample result ( will be

PostgreSQL: Flattening a relation with an array to emit one row per array entry

无人久伴 提交于 2019-12-10 02:39:07
问题 Given a table defined as such: CREATE TABLE test_values(name TEXT, values INTEGER[]); ...and the following values: | name | values | +-------+---------+ | hello | {1,2,3} | | world | {4,5,6} | I'm trying to find a query which will return: | name | value | +-------+-------+ | hello | 1 | | hello | 2 | | hello | 3 | | world | 4 | | world | 5 | | world | 6 | I've reviewed the upstream documentation on accessing arrays, and tried to think about what a solution using the unnest() function would

JOIN on set returning function results

蹲街弑〆低调 提交于 2019-12-09 13:27:52
问题 I am trying to join table and function which returns rows: SELECT p.id, p.name, f.action, f.amount FROM person p JOIN calculate_payments(p.id) f(id, action, amount) ON (f.id = p.id); This function returns 0, 1 or more rows for each id. The query works on PostgreSQL 9.3, but on 9.1 it shows following error: ERROR: invalid reference to FROM-clause entry for table "p" HINT: There is an entry for table "p", but it cannot be referenced from this part of the query I cannot move out calculations

Split string with two delimiters and convert type

大憨熊 提交于 2019-12-06 10:21:33
问题 I have a PL/pgSQL function like this (thanks to the guy who made this possible): CREATE OR REPLACE FUNCTION public.split_string(text, text) RETURNS SETOF text LANGUAGE plpgsql AS $function$ DECLARE pos int; delim_length int := length($2); BEGIN WHILE $1 <> '' LOOP pos := strpos($1,$2); IF pos > 0 THEN RETURN NEXT substring($1 FROM 1 FOR pos - 1); $1 := substring($1 FROM pos + delim_length); ELSE RETURN NEXT $1; EXIT; END IF; END LOOP; RETURN; END; $function$ It splits a string with a

set-valued function called in context that cannot accept a set

怎甘沉沦 提交于 2019-12-06 04:24:27
问题 I am receiving the error: set-valued function called in context that cannot accept a set when executing this function at RETURN QUERY EXECUTE line: PLSQL $ cat lookup_email.pl CREATE OR REPLACE FUNCTION app.lookup_email(ident_id bigint,sess bigint,company_id bigint,email varchar) RETURNS SETOF RECORD as $$ DECLARE rec RECORD; comp_id bigint; server_session bigint; schema_name varchar; query varchar; BEGIN schema_name:='comp' || company_id; select app.session.session into server_session from