postgresql-json

Convert PostgreSQL nested JSON to numeric array in Tableau

假装没事ソ 提交于 2020-06-28 06:09:21
问题 I have a PostgreSQL database containing a table test_table with individual records. First column is a simple store_id , second column meausurement is a nested json. store_id | measurement ---------------------- 0 | {...} The format of the measurement column is as follows: { 'file_info': 'xxxx', 'data': { 'contour_data': { 'X': [-97.0, -97.0, -97.0, -97.0, -97.0, -97.0], 'Y': [-43.0, -41.0, -39.0, -39.0, -38.0, -36.0] } } } I would like to plot Y vs. X in a scatter plot in Tableau. Therefore I

Updating integer column from jsonb member fails with: column is of type integer but expression is of type jsonb

泪湿孤枕 提交于 2020-01-13 22:52:38
问题 In a PostgreSQL 9.5 table I have an integer column social . When I try to update it in a stored procedure given the following JSON data (an array with 2 objects, each having a "social" key) in the in_users variable of type jsonb : '[{"sid":"12345284239407942","auth":"ddddc1808197a1161bc22dc307accccc",**"social":3**,"given":"Alexander1","family":"Farber","photo":"https:\/\/graph.facebook.com\/1015428423940942\/picture?type=large","place":"Bochum, Germany","female":0,"stamp":1450102770}, {"sid"

Updating integer column from jsonb member fails with: column is of type integer but expression is of type jsonb

最后都变了- 提交于 2020-01-13 22:52:21
问题 In a PostgreSQL 9.5 table I have an integer column social . When I try to update it in a stored procedure given the following JSON data (an array with 2 objects, each having a "social" key) in the in_users variable of type jsonb : '[{"sid":"12345284239407942","auth":"ddddc1808197a1161bc22dc307accccc",**"social":3**,"given":"Alexander1","family":"Farber","photo":"https:\/\/graph.facebook.com\/1015428423940942\/picture?type=large","place":"Bochum, Germany","female":0,"stamp":1450102770}, {"sid"

Syntax error in UPSERT test code

两盒软妹~` 提交于 2019-12-12 10:23:11
问题 I am trying to test the new PostgreSQL upsert syntax with the following test code, but get the syntax error: test=> CREATE TABLE test1 ( test(> key1 integer PRIMARY KEY check (key1 > 0), test(> key2 integer check (key2 > 0) test(> ); CREATE TABLE test=> CREATE OR REPLACE FUNCTION upsert(IN in_json_array jsonb) test-> RETURNS void AS test-> $func$ test$> UPDATE test1 t SET test$> t.key1 = (obj->>'key1')::int, test$> t.key2 = (obj->>'key2')::int test$> FROM JSONB_ARRAY_ELEMENTS(in_json_array)

Postgres recursive query with row_to_json

那年仲夏 提交于 2019-11-28 20:57:49
I've got a table in postgres 9.3.5 that looks like this: CREATE TABLE customer_area_node ( id bigserial NOT NULL, customer_id integer NOT NULL, parent_id bigint, name text, description text, CONSTRAINT customer_area_node_pkey PRIMARY KEY (id) ) I query with: WITH RECURSIVE c AS ( SELECT *, 0 as level, name as path FROM customer_area_node WHERE customer_id = 2 and parent_id is null UNION ALL SELECT customer_area_node.*, c.level + 1 as level, c.path || '/' || customer_area_node.name as path FROM customer_area_node join c ON customer_area_node.parent_id = c.id ) SELECT * FROM c ORDER BY path;

Distinct on Postgresql JSON data column

允我心安 提交于 2019-11-28 09:35:51
Trying to do distinct on a mode with rails. 2.1.1 :450 > u.profiles.select("profiles.*").distinct Profile Load (0.9ms) SELECT DISTINCT profiles.* FROM "profiles" INNER JOIN "integration_profiles" ON "profiles"."id" = "integration_profiles"."profile_id" INNER JOIN "integrations" ON "integration_profiles"."integration_id" = "integrations"."id" WHERE "integrations"."user_id" = $1 [["user_id", 2]] PG::UndefinedFunction: ERROR: could not identify an equality operator for type json LINE 1: SELECT DISTINCT profiles.* FROM "profiles" INNER JOIN "integ... ^ : SELECT DISTINCT profiles.* FROM "profiles"

Postgres recursive query with row_to_json

僤鯓⒐⒋嵵緔 提交于 2019-11-27 12:34:43
问题 I've got a table in postgres 9.3.5 that looks like this: CREATE TABLE customer_area_node ( id bigserial NOT NULL, customer_id integer NOT NULL, parent_id bigint, name text, description text, CONSTRAINT customer_area_node_pkey PRIMARY KEY (id) ) I query with: WITH RECURSIVE c AS ( SELECT *, 0 as level, name as path FROM customer_area_node WHERE customer_id = 2 and parent_id is null UNION ALL SELECT customer_area_node.*, c.level + 1 as level, c.path || '/' || customer_area_node.name as path

PostgreSQL: Remove attribute from JSON column

点点圈 提交于 2019-11-27 11:27:43
问题 I need to remove some attributes from a json type column. The Table: CREATE TABLE my_table( id VARCHAR(80), data json); INSERT INTO my_table (id, data) VALUES ( 'A', '{"attrA":1,"attrB":true,"attrC":["a", "b", "c"]}' ); Now, I need to remove attrB from column data . Something like alter table my_table drop column data->'attrB'; would be nice. But a way with a temporary table would be enough, too. 回答1: Update : for 9.5+, there are explicit operators you can use with jsonb (if you have a json

Distinct on Postgresql JSON data column

◇◆丶佛笑我妖孽 提交于 2019-11-27 03:01:58
问题 Trying to do distinct on a mode with rails. 2.1.1 :450 > u.profiles.select("profiles.*").distinct Profile Load (0.9ms) SELECT DISTINCT profiles.* FROM "profiles" INNER JOIN "integration_profiles" ON "profiles"."id" = "integration_profiles"."profile_id" INNER JOIN "integrations" ON "integration_profiles"."integration_id" = "integrations"."id" WHERE "integrations"."user_id" = $1 [["user_id", 2]] PG::UndefinedFunction: ERROR: could not identify an equality operator for type json LINE 1: SELECT

Explanation of JSONB introduced by PostgreSQL

霸气de小男生 提交于 2019-11-26 15:35:54
PostgreSQL just introduced JSONB and it's already trending on hacker news . It would be great if someone could explain how it's different from Hstore and JSON previously present in PostgreSQL. What are it's advantages and limitations and when should someone consider using it? pozs First, hstore is a contrib module, which only allows you to store key => value pairs, where keys and values can only be text s (however values can be sql NULL s too). Both json & jsonb allows you to store a valid JSON value (defined in its spec ). F.ex. these are valid JSON representations: null , true , [1,false,