Sum of json array - json

I have json type field, something like this
data
{"age": 44, "name": "Jun"}
{"age": 19, "name": "Pablo", "attempts": [11, 33, 20]}
{"age": 33, "name": "Maria", "attempts": [77, 10]}
Here some json data have "attempts" array, some not. When json have this array, I need get sum of array elements in different field, need result like
data , sum_of_array
{"age": 44, "name": "Jun"} , (nothing here)
{"age": 19, "name": "Pablo", "attempts": [11, 33, 20]} , 64
{"age": 33, "name": "Maria", "attempts": [77, 10]} , 87

SELECT attempts.id,
sum(vals.v::integer) sum_attempts
FROM attempts
LEFT JOIN LATERAL jsonb_array_elements_text(val->'attempts') vals(v)
ON TRUE
GROUP BY attempts.id;
Use json_array_elements_text if you are using json instead of jsonb.

This works if you have unique id identity column in your table
SELECT your_table.*, tt.sum FROM your_table
LEFT JOIN (
select id, SUM(arrvals) as sum FROM (
select id, json_array_elements_text(CAST(your_json_column->>'attempts' AS json))::NUMERIC as arrvals from your_table
)t
group by id
) tt
ON your_table.id = tt.id

Related

MySQL find in database where value in array in JSON is BETWEEN something

I have database
user
info
0
{"messages": [{"user_to": 1, "timestamp": 1663000000}, {"user_to": 2, "timestamp": 1662000000}]}
1
{"messages": [{"user_to": 0, "timestamp": 1661000000}, {"user_to": 2, "timestamp": 1660000000}]}
2
{"messages": []}
And I want to select all users who sent messages between timestamp 1662000000 and 1663000000 (any amount of messages, not all of them)
I don't have external table of messages, so I can't select from there
If you're using MySQL v8.0.x, you can utilize JSON_TABLE to create a JSON formatted table in a subquery. Then, select your DISTINCT users using your timestamp in a WHERE clause like this:
SELECT DISTINCT b.`user` FROM (
SELECT `user`, a.*
FROM `sample_table`,
JSON_TABLE(`info`,'$'
COLUMNS (
NESTED PATH '$.messages[*]'
COLUMNS (
`user_to` int(11) PATH '$.user_to',
`timestamp` int(40) PATH '$.timestamp')
)
) a
) b
WHERE b.`timestamp` BETWEEN 1662000000 AND 1663000000
ORDER BY b.`user` ASC
Input:
user
info
0
{"messages": [{"user_to": 1, "timestamp": 1663000000}, {"user_to": 2, "timestamp": 1662000000}]}
1
{"messages": [{"user_to": 0, "timestamp": 1661000000}, {"user_to": 2, "timestamp": 1660000000}]}
2
{"messages": []}
3
{"messages": [{"user_to": 0, "timestamp": 1662000000}, {"user_to": 2, "timestamp": 1661000000}, {"user_to": 2, "timestamp": 1660000000}, {"user_to": 2, "timestamp": 1663000000}]}
Output:
user
0
3
db<>fiddle here

Find object by key/value in an array in postgresql jsonb column

I have the following table:
CREATE TABLE api_data (
id bigserial NOT NULL PRIMARY KEY,
content JSONB NOT NULL
);
Now I insert an array like this into the content column:
[{ "id": 44, "name": "address One", "petId": 1234 },
{ "id": 45, "name": "address One", "petId": 1234 },
{ "id": 46, "name": "address One", "petId": 1111 }]
What I want next is to get exactly the objects that have the "petId" set to a given value.
I figured I could do
select val
from api_data
WHERE content #> '[{"petId":1234}]'
But that returns the whole array.
Another thing I found is this query:
select val
from api_data
JOIN LATERAL jsonb_array_elements(content) obj(val) ON obj.val->>'petId' = '1234'
WHERE content #> '[{"petId":1234}]'
Which returns the object I am looking for, but three times which matches the number of elements in the array.
What I actually need is a result like this:
[{ "id": 44, "name": "address One", "petId": 1234 },
{ "id": 45, "name": "address One", "petId": 1234 }]
If you are using Postgres 12, you can use a JSON path expression:
select jsonb_path_query_array(content, '$[*] ? (#.petId == 1234)') as content
from api_data
where content #> '[{"petId":1234}]';
If you are using an older version, you need to unnest and aggregate manually:
select (select jsonb_agg(e)
from jsonb_array_elements(d.content) as t(e)
where t.e #> '{"petId":1234}') as content
from api_data d
where d.content #> '[{"petId":1234}]'

Count json tags in sql

I have this json strings
[{"count": 9, "name": "fixkit", "label": "Repair Kit"}, {"count": 1, "name": "phone", "label": "Telefoon"}]
[{"count": 3, "name": "phone", "label": "Telefoon"}]
[{"count": 5, "name": "kunststof", "label": "Kunststof"}, {"count": 6, "name": "papier", "label": "Papier"}, {"count": 2, "name": "metaal", "label": "Metaal"}, {"count": 2, "name": "inkt", "label": "Inkt"}, {"count": 3, "name": "kabels", "label": "Kabels"}, {"count": 2, "name": "klei", "label": "Klei"}, {"count": 2, "name": "glas", "label": "Glas"}, {"count": 12, "name": "phone", "label": "Telefoon"}]
[{"count": 77, "name": "weed", "label": "Cannabis"}, {"count": 1, "name": "firework1", "label": "Vuurpijl 1"}]
And know i want the following output
Phone | Number of phones (in this case: 16)
Fixkit | Number of fixkits (in this case: 9)
I wanted to do this with a sql query. If you know how to do this, thanks in advance!
If you're not using MySQL 8, this is a bit more complicated. First you have to find a path to a name element that has the value phone (or fixkit); then you can replace name in that path with count and extract the count field from that path; these values can then be summed:
SELECT param, SUM(JSON_EXTRACT(counts, REPLACE(JSON_UNQUOTE(JSON_SEARCH(counts, 'one', param, NULL, '$[*].name')), 'name', 'count'))) AS count
FROM data
CROSS JOIN (
SELECT 'phone' AS param
UNION ALL
SELECT 'fixkit'
) params
WHERE JSON_SEARCH(counts, 'one', param, NULL, '$[*].name') IS NOT NULL
GROUP BY param
Output:
param count
fixkit 9
phone 16
Demo on dbfiddle
If you are running MySQL 8.0, you can unnest the arrays into rows with json_table(), then filter on the names you are interested in, and aggregate.
Assuming that your table is mytable and that the json column is called js, that would be:
select j.name, sum(j.cnt) cnt
from mytable t
cross join json_table (
t.js,
'$[*]' columns(
cnt int path '$.count',
name varchar(50) path '$.name'
)
) j
where j.name in ('phone', 'fixkit')
group by j.name
Demo on DB Fiddle:
| name | cnt |
| ------ | --- |
| fixkit | 9 |
| phone | 16 |

How to ORDER BY a JSON value?

I have this column named "data" and has some JSON in it.
What i want to do is order my SQL query by the "toptimes" value.
My actual and desired query:
"SELECT core_members.pp_thumb_photo,name,member_group_id,data FROM game_accounts.accounts INNER JOIN website_accounts.core_members ON member_id = account_id WHERE member_group_id IN (4, 7, 8, 6) ORDER BY data ->> '$[0].toptimes' ASC LIMIT 100"
My JSON code:
[ { "daily_login": { "yearday": 56, "hour": 11, "second": 33, "minute": 18, "weekday": 3, "month": 1, "monthday": 26, "timestamp": 1582715913, "year": 120, "isdst": 0 }, "toptimes": 49, "daily_login_streak": 1, "hunters": 59, "playtime": 226099647, "awards": [ ], "nickname": "RandomNick" } ]
It has to be something on these lines:
ORDER BY JSON_VALUE(data,'$.daily_login.toptimes)
Access toptimes through daily_login within the JSON object.
Presumably, you want:
order by data ->> '$[0].toptimes'
This will order the resultset according to the value of toptimes in the first element of your JSON array.
If you are storing a JSON object and not an array (although this is not what you showed in yuour sample data), then:
order by data ->> '$.toptimes'
I had a problem, only for MS SQL. It helped to convert a string to a number.
SELECT TOP (1000) [Uuid],
JSON_VALUE(json, '$.likesCount') as likesCount,
FROM [dbo].[Playlists]
order by CONVERT(bigint, JSON_VALUE(json, '$.likesCount')) desc

Query jsonb column to match an array of keys

I have a table items which has a jsonb column data.
The data column is something like this {"name": "aaa", "age": 23, "job": "dev"}.
How do I select items that the data has only the keys name, age?.
You can use the ? and the ?& operators.
For your usecase, it will be:
SELECT * FROM table WHERE (NOT data ? 'job') AND (data ?& array ['name', 'age'])
Use the delete operator -, example:
with items (data) as (
values
('{"name": "aaa", "age": 23}'::jsonb),
('{"name": "aaa", "age": 23, "job": "dev"}'),
('{"name": "aaa", "age": 23, "gender": "f"}')
)
select *
from items
where data - 'name'- 'age' = '{}'
data
----------------------------
{"age": 23, "name": "aaa"}
(1 row)
In Postgres 10+ you can use a text array:
select *
from items
where data - array['name', 'age'] = '{}'