lateral

Lateral Flatten Snowpipe data with mixture of arrays and dict

泪湿孤枕 提交于 2020-04-18 05:27:42
问题 I have two different structured json files being piped in from a snowpipe. The only difference is that instead of a nested dict it has many nested arrays. I am trying to figure out how to transform structure 1 into one finalized table. I've successfully transformed structure 2 into a table and included the code below. I know I need to be making use of lateral flatten but have not been successful. **Structure 1: Nested Arrays (Need help on)** This json lives within a table and in column *

Dynamically execute query using the output of another query

别等时光非礼了梦想. 提交于 2020-01-21 16:57:08
问题 I have a function called generate_table, that takes 2 input parameters ( rundate::date and branch::varchar ) Now I am trying to work on a second function, using PLPGSQL, that will get a list of all branches and the newest date for each branch and pass this as a parameter to the generate_table function. The query that I have is this: select max(rundate) as rundate, branch from t_index_of_imported_files group by branch and it results on this: rundate;branch 2014-03-13;branch1 2014-03-12;branch2

Dynamically execute query using the output of another query

回眸只為那壹抹淺笑 提交于 2020-01-21 16:56:50
问题 I have a function called generate_table, that takes 2 input parameters ( rundate::date and branch::varchar ) Now I am trying to work on a second function, using PLPGSQL, that will get a list of all branches and the newest date for each branch and pass this as a parameter to the generate_table function. The query that I have is this: select max(rundate) as rundate, branch from t_index_of_imported_files group by branch and it results on this: rundate;branch 2014-03-13;branch1 2014-03-12;branch2

“invalid reference to FROM-clause entry for table” in Postgres query

时光毁灭记忆、已成空白 提交于 2020-01-03 20:12:09
问题 I have the following query: query = "SELECT data #>> '{id}' AS id, data #>> '{name}' AS name, data #>> '{curator}' AS curator, data #> '{$isValid}' AS \"$isValid\", data #> '{customer}' AS customer, data #> '{$createdTS}' AS \"$createdTS\", data #> '{$updatedTS}' AS \"$updatedTS\", data #> '{$isComplete}' AS \"$isComplete\", (count(keys))::numeric as \"numProducts\", created_at FROM appointment_intakes, LATERAL jsonb_object_keys(data #> '{products}') keys INNER JOIN appointment_intake_users

How to rewrite a SELECT … CROSS JOIN LATERAL … statement for older PostgreSQL versions?

ぃ、小莉子 提交于 2019-12-25 13:13:55
问题 I am faced with a query similar to this: SELECT * FROM invoicable_interval i, LATERAL partition_into_months(i.start_date, i.stop_or_current_date) p; ... where partition_into_months is defined similar to this: CREATE FUNCTION partition_into_months(start_date DATE, stop_date DATE) RETURNS TABLE (start_date DATE, stop_date DATE, days INT) AS $$ ... $$ LANGUAGE sql IMMUTABLE; So I am doing a cross join with a variable interval for the secondary table, hence the (redundant) keyword LATERAL. This

How to rewrite a SELECT … CROSS JOIN LATERAL … statement for older PostgreSQL versions?

五迷三道 提交于 2019-12-25 13:12:41
问题 I am faced with a query similar to this: SELECT * FROM invoicable_interval i, LATERAL partition_into_months(i.start_date, i.stop_or_current_date) p; ... where partition_into_months is defined similar to this: CREATE FUNCTION partition_into_months(start_date DATE, stop_date DATE) RETURNS TABLE (start_date DATE, stop_date DATE, days INT) AS $$ ... $$ LANGUAGE sql IMMUTABLE; So I am doing a cross join with a variable interval for the secondary table, hence the (redundant) keyword LATERAL. This

How to rewrite a SELECT … CROSS JOIN LATERAL … statement for older PostgreSQL versions?

寵の児 提交于 2019-12-25 13:12:13
问题 I am faced with a query similar to this: SELECT * FROM invoicable_interval i, LATERAL partition_into_months(i.start_date, i.stop_or_current_date) p; ... where partition_into_months is defined similar to this: CREATE FUNCTION partition_into_months(start_date DATE, stop_date DATE) RETURNS TABLE (start_date DATE, stop_date DATE, days INT) AS $$ ... $$ LANGUAGE sql IMMUTABLE; So I am doing a cross join with a variable interval for the secondary table, hence the (redundant) keyword LATERAL. This

plpgsql function that returns multiple columns gets called multiple times

女生的网名这么多〃 提交于 2019-12-14 02:35:13
问题 I'm running PostgreSQL 9.2.1 and have a plpgsql function that returns 3 columns. It's called like this (simplified): SELECT (my_function(b.input)).*, a.other, b.columns FROM table_a a JOIN table_b b ON a.id = b.id WHERE ... The function prints out a WARNING message and I was surprised to find it printed 3 times. It looks like the function gets called 3 times - presumably once for each column. This can't be good for performance! How can I make sure it's called only once? It's already marked

ERROR: function expression in form cannot refer to other relations of same query level : How to work around LATERAL

∥☆過路亽.° 提交于 2019-12-13 08:02:34
问题 DROP SCHEMA tmp CASCADE; CREATE SCHEMA tmp ; SET search_path=tmp; CREATE TABLE primes ( pos SERIAL NOT NULL PRIMARY KEY , val INTEGER NOT NULL , CONSTRAINT primes_alt UNIQUE (val) ); CREATE FUNCTION is_prime(_val INTEGER) RETURNS BOOLEAN AS $func$ DECLARE ret BOOLEAN ; BEGIN SELECT False INTO ret WHERE EXISTS (SELECT * FROM primes ex WHERE ex.val = $1 OR ( (ex.val * ex.val) <= $1 AND ($1 % ex.val) = 0 ) ); RETURN COALESCE(ret, True); END; $func$ LANGUAGE plpgsql STABLE; CREATE VIEW vw_prime

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