postgresql-9.4

Merging Concatenating JSON(B) columns in query

旧城冷巷雨未停 提交于 2019-11-27 12:44:19
问题 Using Postgres 9.4, I am looking for a way to merge two (or more) json or jsonb columns in a query. Consider the following table as an example: id | json1 | json2 ---------------------------------------- 1 | {'a':'b'} | {'c':'d'} 2 | {'a1':'b2'} | {'f':{'g' : 'h'}} Is it possible to have the query return the following: id | json ---------------------------------------- 1 | {'a':'b', 'c':'d'} 2 | {'a1':'b2', 'f':{'g' : 'h'}} Unfortunately, I can't define a function as described here. Is this

Referencing current row in FILTER clause of window function

拥有回忆 提交于 2019-11-27 02:42:21
问题 In PostgreSQL 9.4 the window functions have the new option of a FILTER to select a sub-set of the window frame for processing. The documentation mentions it, but provides no sample. An online search yields some samples, including from 2ndQuadrant but all that I found were rather trivial examples with constant expressions. What I am looking for is a filter expression that includes the value of the current row. Assume I have a table with a bunch of columns, one of which is of date type: col1 |

jsonb query with nested objects in an array

百般思念 提交于 2019-11-27 02:20:44
问题 I'm using PostgreSQL 9.4 with a table teams containing a jsonb column named json . I am looking for a query where I can get all teams which have the Players 3 , 4 and 7 in their array of players. The table contains two rows with the following json data: First row: { "id": 1, "name": "foobar", "members": { "coach": { "id": 1, "name": "A dude" }, "players": [ { "id": 2, "name": "B dude" }, { "id": 3, "name": "C dude" }, { "id": 4, "name": "D dude" }, { "id": 6, "name": "F dude" }, { "id": 7,

How to perform update operations on columns of type JSONB in Postgres 9.4

混江龙づ霸主 提交于 2019-11-26 18:28:29
Looking through the documentation for the Postgres 9.4 datatype JSONB, it is not immediately obvious to me how to do updates on JSONB columns. Documentation for JSONB types and functions: http://www.postgresql.org/docs/9.4/static/functions-json.html http://www.postgresql.org/docs/9.4/static/datatype-json.html As an examples, I have this basic table structure: CREATE TABLE test(id serial, data jsonb); Inserting is easy, as in: INSERT INTO test(data) values ('{"name": "my-name", "tags": ["tag1", "tag2"]}'); Now, how would I update the 'data' column? This is invalid syntax: UPDATE test SET data->

Flatten aggregated key/value pairs from a JSONB field?

你离开我真会死。 提交于 2019-11-26 14:38:22
I am working in Postgres 9.4 with the following table: Column │ Type │ Modifiers ─────────────────┼──────────────────────┼────────────────────── id │ integer │ not null default practice_id │ character varying(6) │ not null date │ date │ not null pct_id │ character varying(3) │ total_list_size │ double precision │ not null star_pu │ jsonb │ I have the following query: SELECT date, AVG(total_list_size) AS total_list_size, json_object_agg(key, val) AS star_pu FROM (SELECT date, SUM(total_list_size) AS total_list_size, key, SUM(value::numeric) val FROM frontend_practicelist p, jsonb_each_text(star

Flatten aggregated key/value pairs from a JSONB field?

99封情书 提交于 2019-11-26 03:40:02
问题 I am working in Postgres 9.4 with the following table: Column │ Type │ Modifiers ─────────────────┼──────────────────────┼────────────────────── id │ integer │ not null default practice_id │ character varying(6) │ not null date │ date │ not null pct_id │ character varying(3) │ total_list_size │ double precision │ not null star_pu │ jsonb │ I have the following query: SELECT date, AVG(total_list_size) AS total_list_size, json_object_agg(key, val) AS star_pu FROM (SELECT date, SUM(total_list