Delete a value from jsonb array data having no key in postgresql - postgresql

Table structure is:
CREATE TABLE mine_check.meta
(
sl_no bigserial NOT NULL,
tags jsonb NOT NULL DEFAULT '[]'::jsonb
);
Table looks like
sl.no tags
1 [120,450]
2 [120]
3 [450,980,120]
4 [650]
I need to delete 120 from the tags column - having no key
I tried reading many places - there they had key to update or delete.
How should I progress ?

I am afraid that it has to be done the hard way - unnest the JSONB array, select and filter from it and aggregate back into a JSONB array.
select sl_no,
(
select jsonb_agg(e::integer)
from jsonb_array_elements_text(tags) e
where e <> 120::text
) tags
from mine_check.meta;

Related

PostgreSQL: Filter and Aggregate on JSONB Array type

Consider the following table definition:
CREATE TABLE keys
(
id bigint NOT NULL DEFAULT nextval('id_seq'::regclass),
key_value jsonb[] NOT NULL DEFAULT ARRAY[]::jsonb[],
)
The table now contains the following values:
id | key_value
---|-----------
1 | {"{\"a\": \1\", \"b\": \"2\", \"c\": \"3\"}","{\"a\": \"4\", \"b\": \"5\", \"c\": \"6\"}","{\"a\": \"7\", \"b\": \"8\", \"c\": \"9\"}"} |
How do I:
Select all rows where value of b is NOT 2? I tried using the #> operator,
For the returned rows, for each key_value object, return c - a
My confusion stems from the fact that all methods dealing with JSONB in postgres seem to accept JSON or JSONB but none seem to work with JSONB[]. Not sure what I am missing?
Thanks in advance
What could be better than doing this with unnest and normal relational operations?
array types and json are abominations in the face of the perfection that is relational sets. The first rule of holes is that when you find yourself in one, stop digging and climb out of the hole.
with unwind as (
select id, unnest(key_value) as kvjson
from keys
)
select id, (kvjson->>'c')::int - (kvjson->>'a')::int as difference
from unwind
where kvjson->>'b' != '2';

postgresql : search records based on array field vaule with multiple values

I have a table that has an array field.
CREATE TABLE notifications
(
id integer NOT NULL DEFAULT nextval('notifications_id_seq'::regclass),
title character(100) COLLATE pg_catalog."default" NOT NULL,
tags text[] COLLATE pg_catalog."default",
CONSTRAINT notifications_pkey PRIMARY KEY (id)
)
and tags field can have multiple values from
["a","b","c","d"]
now I want all the records for which tags have a or d ("a","d")array values.
I can use postgresl in but this can be used to search single value. How can I achieve this?
You could use ANY:
SELECT *
FROM notifications
WHERE 'a' = ANY(tags) OR 'b' = ANY(tags);
DBFiddle Demo
If the values 'a' and 'b' are static (you only need to check for those 2 values in every query), then you can go with the solution that Lukasz Szozda provided.
But if the values you want to check for are dynamic and are different in multiple queries(sometimes it is {'a','b'} but sometimes it is {'b', 'f','m'}) you can create an intersection of both of the arrays and check if the intersection is empty.
For example:
If we have the following table and data:
CREATE TABLE test_table_1(description TEXT, tags TEXT[]);
INSERT INTO test_table_1(description, tags) VALUES
('desc1', array['a','b','c']),
('desc2', array['c','d','e']);
If we want to get all of the rows from test_table_1 that have one of the following tags b, f, or m, we could do it with the following query:
SELECT * FROM test_table_1 tt1
WHERE array_length((SELECT array
(
SELECT UNNEST(tt1.tags)
INTERSECT
SELECT UNNEST(array['b','f','m'])
)), 1) > 0;
In the query above we use array_length to check if the intersection is empty.
Writing the query this way can also be useful if you want to add additional constraint to the number of matched tags.
For example if you want to get all of the rows that have at least 2 tags from the group {'a','b','c'} you just need to set array_length(...) > 1

Array of array in Postgres

In a Postgres DB I have a field field defined like this:
CREATE TABLE t (
id SERIAL PRIMARY KEY,
field character varying(255)[] DEFAULT ARRAY[]::character varying[],
);
There I store values like:
ID FIELD
1 {{lower,0},{greater,10}}
2 {{something_else,7},{lower,5}}
1 - How can I select the lower/greater value? I'd like a query response like this:
ID LOWER
1 0
2 5
2 - How can I filter by those lower/greater values?
Thanks!
It's pretty awkward to do but this accomplishes it. I use PG 9.3 so I don't know if there are better ways to do this in later versions.
SELECT id, (SELECT field[ss][2] FROM generate_subscripts(field, 1) ss WHERE field[ss][1] = 'lower') AS lower
FROM t;
Basically, for each record, generate the subscripts to use as indexes into the main array to access the subarrays. For each, look for an array where the first item is 'lower'. If found, return the value of the second item.

How to get size of PostgreSQL jsonb field?

I have a table with jsonb field in table.
CREATE TABLE data.items
(
id serial NOT NULL,
datab jsonb
)
How to get size of this field in a query like this:
select id, size(datab) from data.items
For the number of bytes used to store:
select id, pg_column_size(datab) from data.items;
For the number of elements on the jsonb object:
select id, jsonb_array_length(datab) from data.items;
If the column uses EXTENDED storage (TOAST compression), you should use octet_length(datab::text) instead of pg_column_size

CSV file data into a PostgreSQL table

I am trying to create a database for movielens (http://grouplens.org/datasets/movielens/). We've got movies and ratings. Movies have multiple genres. I splitted those out into a separate table since it's a 1:many relationship. There's a many:many relationship as well, users to movies. I need to be able to query this table multiple ways.
So I created:
CREATE TABLE genre (
genre_id serial NOT NULL,
genre_name char(20) DEFAULT NULL,
PRIMARY KEY (genre_id)
)
.
INSERT INTO genre VALUES
(1,'Action'),(2,'Adventure'),(3,'Animation'),(4,'Children\s'),(5,'Comedy'),(6,'Crime'),
(7,'Documentary'),(8,'Drama'),(9,'Fantasy'),(10,'Film-Noir'),(11,'Horror'),(12,'Musical'),
(13,'Mystery'),(14,'Romance'),(15,'Sci-Fi'),(16,'Thriller'),(17,'War'),(18,'Western');
.
CREATE TABLE movie (
movie_id int NOT NULL DEFAULT '0',
movie_name char(75) DEFAULT NULL,
movie_year smallint DEFAULT NULL,
PRIMARY KEY (movie_id)
);
.
CREATE TABLE moviegenre (
movie_id int NOT NULL DEFAULT '0',
genre_id tinyint NOT NULL DEFAULT '0',
PRIMARY KEY (movie_id, genre_id)
);
I dont know how to import my movies.csv with columns movie_id, movie_name and movie_genre For example, the first row is (1;Toy Story (1995);Animation|Children's|Comedy)
If I INSERT manually, it should be look like:
INSERT INTO moviegenre VALUES (1,3),(1,4),(1,5)
Because 3 is Animation, 4 is Children and 5 is Comedy
How can I import all data set this way?
You should first create a table that can ingest the data from the CSV file:
CREATE TABLE movies_csv (
movie_id integer,
movie_name varchar,
movie_genre varchar
);
Note that any single quotes (Children's) should be doubled (Children''s). Once the data is in this staging table you can copy the data over to the movie table, which should have the following structure:
CREATE TABLE movie (
movie_id integer, -- A primary key has implicit NOT NULL and should not have default
movie_name varchar NOT NULL, -- Movie should have a name, varchar more flexible
movie_year integer, -- Regular integer is more efficient
PRIMARY KEY (movie_id)
);
Sanitize your other tables likewise.
Now copy the data over, extracting the unadorned name and the year from the CSV name:
INSERT INTO movie (movie_id, movie_name)
SELECT parts[1], parts[2]::integer
FROM movies_csv, regexp_matches(movie_name, '([[:ascii:]]*)\s\(([\d]*)\)$') p(parts)
Here the regular expression says:
([[:ascii:]]*) - Capture all characters until the matches below
\s - Read past a space
\( - Read past an opening parenthesis
([\d]*) - Capture any digits
\) - Read past a closing parenthesis
$ - Match from the end of the string
So on input "Die Hard 17 (John lives forever) (2074)" it creates a string array with {'Die Hard 17 (John lives forever)', '2074'}. The scanning has to be from the end $, assuming all movie titles end with the year of publication in parentheses, in order to preserve parentheses and numbers in movie titles.
Now you can work on the movie genres. You have to split the string on the bar | using the regex_split_to_table() function and then join to the genre table on the genre name:
INSERT INTO moviegenre
SELECT movie_id, genre_id
FROM movies_csv, regexp_split_to_table(movie_genre, '\|') p(genre) -- escape the |
JOIN genre ON genre.genre_name = p.genre;
After all is done and dusted you can delete the movies_csv table.