PostgreSQL find by value in array in jsonb data - postgresql

How can I get records from table where array in column value contains any value to find.
Well, the column can contain any data type of array, objects, strings, etc and null value. And arrays in column can contain any serializable data type
id|value |
--+------------+
1|null |
2|[0.05, 0.11]|

You can use a JSON path expression:
select *
from the_table
where value ## '$[*] == 0.11'
If the column doesn't contain an array, you can use
select *
from the_table
where value ## '$.* == 0.11'
This assumes value is defined as jsonb (which it should be). If it's not, you have to cast it value::jsonb
Online example

Some samples:
-- sample 1
with sample_data as (
select 1 as "id", null::jsonb as "value"
union all
select 2 as "id", '[0.05, 0.11]'::jsonb as "value"
)
select a2.pval::float4 from sample_data as a1
cross join jsonb_array_elements(a1."value") as a2(pval)
--Return:
0.05
0.11
-- sample 2
with sample_data as (
select 1 as "id", null::jsonb as "value"
union all
select 2 as "id", '[0.05, 0.11]'::jsonb as "value"
)
select a2.pval::float4 from sample_data as a1
cross join jsonb_array_elements(a1."value") as a2(pval)
where a2.pval::float4 > 0.1
--Return:
0.11

Related

How to select rows where the condition where all rows are being extracted for a given condition?

I have this table
CREATE TABLE fruits
(
id SERIAL,
name VARCHAR
);
with these entries
INSERT INTO fruits(name)
VALUES('Orange');
INSERT INTO fruits(name)
VALUES('Ananas');
INSERT INTO fruits(name)
VALUES(null);
When I try to to select all rows that not equal to 'Ananas' by querying
select *
from fruits
where name <> 'Ananas'
I get these rows:
id name
-----------
1 Orange
What I would have expected was this
id name
-----------
1 Orange
3 null
How do I ensure that all rows that fulfills the condition gets selected?
Example in dbfiddle:
https://dbfiddle.uk/?rdbms=postgres_11&fiddle=a963d39df0466701b0a96b20db8461e6
Any "normal" comparison with null yields "unknown" which is treated as false in the context of the WHERE clause.
You need to use the null safe operator is distinct from:
select *
from fruits
where name is distinct from 'Ananas';
Alternatively you could convert NULL values to something different:
select *
from fruits
where coalesce(name, '') <> 'Ananas';

Postgresql Convert Json Object to Columns

I am using postgresql database. I have a column which is jsonb data type.
For example I have a json data like below:
{
"test_question_number": ["1000000000", "5000000000"],
"question1": 0.04975124378109453,
"question2": 5.077114427860696,
"question3": 75621.89054726369,
"question4": 3482.587064676617,
"question6": 1,
"question8": 0.000176068
}
As you see it is key value json data. And the data can be different, So the key names are not same for other saved json data.
Now I would like to convert it as colum and row. Like below:
---------------------------------------------------------------------------------------
| |test_question_number |question1| |question2| |question3|
---------------------------------------------------------------------------------------
| 1 | "1000000000" | 0.04975124378109453| 5.077114427860696 |75621.89054726369
------------------------ --------------------------------------------------------------
| 2 | "5000000000" | | |
---------------------------------------------------------------------------------------
I have tried jsonb_build_object, jsonb_populate_recordset and some function but I could not solve.
A static pivoting solution might be
WITH t AS
(
SELECT JSONB_TYPEOF(value::JSONB) AS type, js.*
FROM t
CROSS JOIN JSONB_EACH(jsdata) AS js
)
SELECT arr.*, question1, question2, question3, question4, question6, question8
FROM
(
SELECT row_id, test_question_number
FROM t
CROSS JOIN JSONB_ARRAY_ELEMENTS(value::JSONB)
WITH ORDINALITY arr(test_question_number,row_id)
WHERE type = 'array' ) AS arr
LEFT JOIN
( SELECT cnt, MAX(value::text) FILTER (WHERE key = 'question1') AS question1,
MAX(value::text) FILTER (WHERE key = 'question2') AS question2,
MAX(value::text) FILTER (WHERE key = 'question3') AS question3,
MAX(value::text) FILTER (WHERE key = 'question4') AS question4,
MAX(value::text) FILTER (WHERE key = 'question6') AS question6,
MAX(value::text) FILTER (WHERE key = 'question8') AS question8
FROM (SELECT t.*, COUNT(*) OVER (PARTITION BY key) AS cnt
FROM t
WHERE type != 'array'
) AS q
GROUP BY cnt ) AS obj
ON arr.row_id = obj.cnt
distinguishing the elements by types as JSON objects whether an array or non-array
Demo

PostgreSQL Crosstab issues / "Return and SQL tuple descriptions are incompatible"

Good afternoon, I am using POSTGRESql version 9.2 and I'm trying to use a crosstab function to transpose two columns on a table so that i can later join it to a different SELECT query.
I have installed the tablefunc extension.
However i keep getting this "Return and SQL tuple descriptions are incompatible" error which seems to be because of typecasts.
I don't need them to be a specific type.
My original SELECT query is this
SELECT inventoryid, ttype, tamount
FROM inventorytesting
Which gives me the following result:
inventoryid ttype tamount
2451530088940460 7 0.2
2451530088940460 2 0.5
2451530088940460 8 0.1
2451530088940460 1 15.7
8751530077940461 7 0.7
8751530077940461 2 0.2
8751530077940461 8 1.1
8751530077940461 1 19.2
and my goal is to get it like:
inventoryid 7 2 8 1
8751530077940461 0.7 0.2 1.1 19.2
2451530088940460 0.2 0.5 0.1 15.7
The 'ttype' field has 49 different values such as "7","2","8","1" which are fixed.
The 'tamount' field varies its values depending on the 'inventoryid' field but there will always be 49 of them, even if its value is zero. It will never be "null".
I have tried a few variations that i could find in the internet which sum up to this:
SELECT *
FROM crosstab (
$$SELECT inventoryid, ttype, tamount
FROM inventorytesting
WHERE inventoryid = '2451530088940460'
ORDER BY inventoryid, ttype$$
)
AS ct("inventoryid" text,"ttype" smallint,"tamount" numeric)
The fieldtypes on the inventorytesting table are
select column_name, data_type from information_schema.columns
where table_name = 'inventorytesting'
Results:
column_name data_type
id bigint
ttype smallint
tamount numeric
tunit text
tlessthan smallint
plantid text
sessiontime bigint
deleted smallint
inventoryid text
docdata text
docname text
labid bigint
Any pointers would be great.
demo:db<>fiddle
The resulting table definition has to contain the table structure you are expecting - the pivoted one - and not the structure of the given one:
SELECT *
FROM crosstab(
$$SELECT inventoryid, ttype, tamount
FROM inventorytesting
WHERE inventoryid = '2451530088940460'
ORDER BY inventoryid, ttype$$
)
AS ct("inventoryid" text,"type1" numeric,"type2" numeric,"type7" numeric,"type8" numeric)
Addionally there is no need to use the crosstab function. You can achieve a pivot by simply using the standard CASE function:
SELECT
inventoryid,
SUM(CASE WHEN ttype = 1 THEN tamount END) AS type1,
SUM(CASE WHEN ttype = 2 THEN tamount END) AS type2,
SUM(CASE WHEN ttype = 7 THEN tamount END) AS type7,
SUM(CASE WHEN ttype = 8 THEN tamount END) AS type8
FROM
inventorytesting
GROUP BY 1
If you were on 9.4 or higher you could use the Postgres specific FILTER clause:
SELECT
inventoryid,
SUM(tamount) FILTER (WHERE ttype = 1) AS type1,
SUM(tamount) FILTER (WHERE ttype = 2) AS type2,
SUM(tamount) FILTER (WHERE ttype = 7) AS type7,
SUM(tamount) FILTER (WHERE ttype = 8) AS type8
FROM
inventorytesting
GROUP BY 1
demo:db<>fiddle
With the crosstab, you define the actual result table (basically the result of the pivot). The input query defines three columns which are then processed as:
grouping column result in the actual rows
the pivot columns
value for the pivot column
In your case, the crosstab therefore needs to be defined as:
ct(
"inventoryid" text,
"tamount_1" numeric,
"tamount_2" numeric,
"tamount_3" numeric,
...
)
The column header will then correlate to a certain value of column ttype in the order as defined by the inner query's ORDER BY.
The thing with crosstab is that missing values for ttype (e.g. some value returned for 4 but not for 3), the resulting columns would be 1, 2, 4, ... with 3 being missing. Here, you'd have to make sure (if you need consistent output) that your inner query returns at least a NULL row (e.g. via LEFT JOIN).

Postgresql: Get records having similar column values

Table A
id name keywords
1 Obj1 a,b,c,austin black
2 Obj2 e,f,austin black,h
3 Obj3 k,l,m,n
4 Obj4 austin black,t,u,s
5 Obj5 z,r,q,w
I need to get those records which contains similar type of keywords. Hence the result for the table needs to be:
Records:
1,2,4
Since records 1,2,4 are the one whose some or the other keyword match with at least any other keyword.
You can convert the "csv" to an array and then use Postgres' array functions:
select *
from the_table t1
where exists (select *
from the_table t2
where string_to_array(t1.keywords, ',') && string_to_array(t2.keywords, ',')
and t1.id <> t2.id);

How to merge JSONB field in a tree structure?

I have a table in Postgres which stores a tree structure. Each node has a jsonb field: params_diff:
CREATE TABLE tree (id INT, parent_id INT, params_diff JSONB);
INSERT INTO tree VALUES
(1, NULL, '{ "some_key": "some value" }'::jsonb)
, (2, 1, '{ "some_key": "other value", "other_key": "smth" }'::jsonb)
, (3, 2, '{ "other_key": "smth else" }'::jsonb);
The thing I need is to select a node by id with additional generated params field which contains the result of merging all params_diff from the whole parents chain:
SELECT tree.*, /* some magic here */ AS params FROM tree WHERE id = 3;
id | parent_id | params_diff | params
----+-----------+----------------------------+-------------------------------------------------------
3 | 2 | {"other_key": "smth else"} | {"some_key": "other value", "other_key": "smth else"}
Generally, a recursive CTE can do the job. Example:
Use table alias in another query to traverse a tree
We just need a more magic to decompose, process and re-assemble the JSON result. I am assuming from your example, that you want each key once only, with the first value in the search path (bottom-up):
WITH RECURSIVE cte AS (
SELECT id, parent_id, params_diff, 1 AS lvl
FROM tree
WHERE id = 3
UNION ALL
SELECT t.id, t.parent_id, t.params_diff, c.lvl + 1
FROM cte c
JOIN tree t ON t.id = c.parent_id
)
SELECT id, parent_id, params_diff
, (SELECT json_object(array_agg(key ORDER BY lvl)
, array_agg(value ORDER BY lvl))::jsonb
FROM (
SELECT key, value
FROM (
SELECT DISTINCT ON (key)
p.key, p.value, c.lvl
FROM cte c, jsonb_each_text(c.params_diff) p
ORDER BY p.key, c.lvl
) sub1
ORDER BY lvl
) sub2
) AS params
FROM cte
WHERE id = 3;
How?
Walk the tree with a classic recursive CTE.
Create a derived table with all keys and values with jsonb_each_text() in a LATERAL JOIN, remember the level in the search path (lvl).
Use DISTINCT ON to get the "first" (lowest lvl) value for each key. Details:
Select first row in each GROUP BY group?
Sort and aggregate resulting keys and values and feed the arrays to json_object() to build the final params value.
SQL Fiddle (only as far as pg 9.3 can go with json instead of jsonb).