set-returning-functions

Split string with two delimiters and convert type

十年热恋 提交于 2019-12-04 12:42:54
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 delimiter. Like this: select * from split_string('3.584731 60.739211,3.590472 60.738030,3.592740 60.736220', '

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

十年热恋 提交于 2019-12-04 09:47:25
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 app.session where app.session.identity_id=ident_id and app.session.session=sess; IF FOUND THEN BEGIN

Postgres function returning one record while I have many records?

元气小坏坏 提交于 2019-12-04 03:54:38
问题 I have many records which my simple query returning but when i use function it just gives me first record, firstly i create my own data type using, CREATE TYPE my_type (usr_id integer , name varchar(30)); and my function is, CREATE OR REPLACE function test() returns my_type as $$ declare rd varchar := '21'; declare personphone varchar := NULL; declare result my_type; declare SQL VARCHAR(300):=null; DECLARE radiophone_clause text = ''; BEGIN IF rd IS NOT NULL then radiophone_clause = 'and pp

Postgres function returning one record while I have many records?

别来无恙 提交于 2019-12-01 20:47:48
I have many records which my simple query returning but when i use function it just gives me first record, firstly i create my own data type using, CREATE TYPE my_type (usr_id integer , name varchar(30)); and my function is, CREATE OR REPLACE function test() returns my_type as $$ declare rd varchar := '21'; declare personphone varchar := NULL; declare result my_type; declare SQL VARCHAR(300):=null; DECLARE radiophone_clause text = ''; BEGIN IF rd IS NOT NULL then radiophone_clause = 'and pp.radio_phone = '|| quote_literal(rd); END IF; IF personphone IS NOT NULL then radiophone_clause =

NULL emements lost when casting result of unnest()

点点圈 提交于 2019-12-01 18:54:28
I stumbled upon very odd behavior with unnest() , when casting after expanding an array. Introduction There are three basic syntax variants to use unnest(): 1) SELECT unnest('{1,NULL,4}'::int[]) AS i; 2) SELECT i FROM unnest('{2,NULL,4}'::int[]) AS i; 3) SELECT i FROM (SELECT unnest('{3,NULL,4}'::int[])) AS t(i); All of them include a row with NULL in the result as expected i --- 1 (null) 4 To cast the array elements to a different type, one can cast the elements to a basic type right after expanding the array, or cast the array itself to a different array type before expanding . The first

NULL emements lost when casting result of unnest()

孤街醉人 提交于 2019-12-01 18:09:41
问题 I stumbled upon very odd behavior with unnest(), when casting after expanding an array. Introduction There are three basic syntax variants to use unnest(): 1) SELECT unnest('{1,NULL,4}'::int[]) AS i; 2) SELECT i FROM unnest('{2,NULL,4}'::int[]) AS i; 3) SELECT i FROM (SELECT unnest('{3,NULL,4}'::int[])) AS t(i); All of them include a row with NULL in the result as expected i --- 1 (null) 4 To cast the array elements to a different type, one can cast the elements to a basic type right after

PostgreSQL - order by an array

那年仲夏 提交于 2019-11-30 05:59:51
问题 I have 2 tables - course that contains id and name of the courses and tagCourse that contains tags for each course. course tagcourse ------------ ---------------- PK id_course PK tag name PK, FK id_course I'd like to write a function that searches courses by given array of tags and returns them ordered by quantity of matching tags. However I don't know how to write it correctly and in an efficient way. Please help me. ie. CREATE OR REPLACE FUNCTION searchByTags(tags varchar[]) RETURNS SETOF..

Error while using regexp_split_to_table (Amazon Redshift)

我怕爱的太早我们不能终老 提交于 2019-11-29 04:34:21
I have the same question as this: Splitting a comma-separated field in Postgresql and doing a UNION ALL on all the resulting tables Just that my 'fruits' column is delimited by '|'. When I try: SELECT yourTable.ID, regexp_split_to_table(yourTable.fruits, E'|') AS split_fruits FROM yourTable I get the following: ERROR: type "e" does not exist Q1. What does the E do? I saw some examples where E is not used. The official docs don't explain it in their "quick brown fox..." example. Q2. How do I use '|' as the delimiter for my query? Edit: I am using PostgreSQL 8.0.2. unnest() and regexp_split_to

Postgresql generate_series of months

只谈情不闲聊 提交于 2019-11-28 20:41:19
I'm trying to generate a series in PostgreSQL with the generate_series function. I need a series of months starting from Jan 2008 until current month + 12 (a year out). I'm using and restricted to PostgreSQL 8.3.14 (so I don't have the timestamp series options in 8.4). I know how to get a series of days like: select generate_series(0,365) + date '2008-01-01' But I am not sure how to do months. select DATE '2008-01-01' + (interval '1' month * generate_series(0,11)) Edit If you need to calculate the number dynamically, the following could help: select DATE '2008-01-01' + (interval '1' month *

PostgreSQL - order by an array

耗尽温柔 提交于 2019-11-28 12:22:50
I have 2 tables - course that contains id and name of the courses and tagCourse that contains tags for each course. course tagcourse ------------ ---------------- PK id_course PK tag name PK, FK id_course I'd like to write a function that searches courses by given array of tags and returns them ordered by quantity of matching tags. However I don't know how to write it correctly and in an efficient way. Please help me. ie. CREATE OR REPLACE FUNCTION searchByTags(tags varchar[]) RETURNS SETOF..... RETURN QUERY SELECT * FROM course c INNER JOIN tagcourse tc ON c.id_course = tc.id_course WHERE ???