set-returning-functions

Function to loop through and select data from multiple tables

◇◆丶佛笑我妖孽 提交于 2019-11-27 20:54:23
I'm new to Postgres and have a database with multiple tables of the same structure. I need to select data from each table that matches certain criteria. I could do this with a bunch of UNION queries, but the number of tables I need to search can change over time, so I don't want to hard code it like that. I've been trying to develop a function that will loop through specific tables (they have a common naming convention) and return a table of records, but I'm not getting any results when I query the function. Function code is below: CREATE OR REPLACE FUNCTION public.internalid_formaltable_name

Function to loop through and select data from multiple tables

给你一囗甜甜゛ 提交于 2019-11-27 19:10:56
问题 I'm new to Postgres and have a database with multiple tables of the same structure. I need to select data from each table that matches certain criteria. I could do this with a bunch of UNION queries, but the number of tables I need to search can change over time, so I don't want to hard code it like that. I've been trying to develop a function that will loop through specific tables (they have a common naming convention) and return a table of records, but I'm not getting any results when I

Postgresql generate_series of months

你。 提交于 2019-11-27 13:04:41
问题 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. 回答1: select DATE '2008-01-01' + (interval '1' month * generate_series(0,11)) Edit If you need to

Parallel unnest() and sort order in PostgreSQL

本秂侑毒 提交于 2019-11-27 09:18:33
I understand that using SELECT unnest(ARRAY[5,3,9]) as id without an ORDER BY clause, the order of the result set is not guaranteed. I could for example get: id -- 3 5 9 But what about the following request: SELECT unnest(ARRAY[5,3,9]) as id, unnest(ARRAY(select generate_series(1, array_length(ARRAY[5,3,9], 1)))) as idx ORDER BY idx ASC Is it guaranteed that the 2 unnest() calls (which have the same length) will unroll in parallel and that the index idx will indeed match the position of the item in the array? I am using PostgreSQL 9.3.3. Erwin Brandstetter Yes, that is a feature of Postgres

Record returned from function has columns concatenated

浪尽此生 提交于 2019-11-27 02:12:25
I have a table which stores account changes over time. I need to join that up with two other tables to create some records for a particular day, if those records don't already exist. To make things easier (I hope), I've encapsulated the query that returns the correct historical data into a function that takes in an account id, and the day. If I execute "Select * account_servicetier_for_day(20424, '2014-08-12')" , I get the expected result (all the data returned from the function in separate columns). If I use the function within another query, I get all the columns joined into one: ("2014-08

Split column into multiple rows in Postgres

≡放荡痞女 提交于 2019-11-27 01:56:03
Suppose I have a table like this: subject | flag ----------------+------ this is a test | 2 subject is of type text , and flag is of type int . I would like to transform this table to something like this in Postgres: token | flag ----------------+------ this | 2 is | 2 a | 2 test | 2 Is there an easy way to do this? In Postgres 9.3+ use a LATERAL join: SELECT s.token, flag FROM tbl t, unnest(string_to_array(t.subject, ' ')) s(token) WHERE flag = 2; Note that the shorthand form of a LATERAL join only returns rows, if unnest() actually returns row(s). You could also use regexp_split_to_table() ,

What's the proper index for querying structures in arrays in Postgres jsonb?

旧街凉风 提交于 2019-11-27 00:40:38
I'm experimenting with keeping values like the following in a Postgres jsonb field in Postgres 9.4: [{"event_slug":"test_1","start_time":"2014-10-08","end_time":"2014-10-12"}, {"event_slug":"test_2","start_time":"2013-06-24","end_time":"2013-07-02"}, {"event_slug":"test_3","start_time":"2014-03-26","end_time":"2014-03-30"}] I'm executing queries like: SELECT * FROM locations WHERE EXISTS ( SELECT 1 FROM jsonb_array_elements(events) AS e WHERE ( e->>'event_slug' = 'test_1' AND ( e->>'start_time' >= '2014-10-30 14:04:06 -0400' OR e->>'end_time' >= '2014-10-30 14:04:06 -0400' ) ) ) How would I

Parallel unnest() and sort order in PostgreSQL

空扰寡人 提交于 2019-11-26 17:49:24
问题 I understand that using SELECT unnest(ARRAY[5,3,9]) as id without an ORDER BY clause, the order of the result set is not guaranteed. I could for example get: id -- 3 5 9 But what about the following request: SELECT unnest(ARRAY[5,3,9]) as id, unnest(ARRAY(select generate_series(1, array_length(ARRAY[5,3,9], 1)))) as idx ORDER BY idx ASC Is it guaranteed that the 2 unnest() calls (which have the same length) will unroll in parallel and that the index idx will indeed match the position of the

Unnest multiple arrays in parallel

瘦欲@ 提交于 2019-11-26 14:42:19
My last question Passing an array to stored to postgres was a bit unclear. Now, to clarify my objective: I want to create an Postgres stored procedure which will accept two input parameters. One will be a list of some amounts like for instance (100, 40.5, 76) and the other one will be list of some invoices ('01-2222-05','01-3333-04','01-4444-08') . After that I want to use these two lists of numbers and characters and do something with them. For example I want to take each amount from this array of numbers and assign it to corresponding invoice. Something like that in Oracle would look like

Split column into multiple rows in Postgres

给你一囗甜甜゛ 提交于 2019-11-26 14:11:46
问题 Suppose I have a table like this: subject | flag ----------------+------ this is a test | 2 subject is of type text , and flag is of type int . I would like to transform this table to something like this in Postgres: token | flag ----------------+------ this | 2 is | 2 a | 2 test | 2 Is there an easy way to do this? 回答1: In Postgres 9.3+ use a LATERAL join: SELECT s.token, flag FROM tbl t, unnest(string_to_array(t.subject, ' ')) s(token) WHERE flag = 2; Note that the shorthand form of a