I have the following dataset here
id
key
category
avg_time_1
avg_time_2
1
1
10
10
20
2
1
20
30
40
3
2
10
10
50
4
2
20
60
70
Sqlfiddle.
I want to create a query that the result will be as follow:
key
avg_time_1
avg_time_1
1
[{ "category": 10, "avg_time": 10},{ "category": 20, "avg_time": 20 }]
[{"category": 10, "avg_time": 20}, {"category": 20, "avg_time": 40}]
2
[{ "category": 10, "avg_time": 10},{ "category": 20, "avg_time": 60}]
[{"category": 10, "avg_time": 50}, {"category": 20, "avg_time": 70}]
The idea is just to re-present the values cols avg_time_1 and avg_time_2 in a different way, as jsonb values.
Use jsonb_build_object() to build prime objects and jsonb_agg() to aggregate them into a json array:
select
key,
jsonb_agg(jsonb_build_object('category', category, 'avg_time', avg_time_1)) as avg_time_1,
jsonb_agg(jsonb_build_object('category', category, 'avg_time', avg_time_2)) as avg_time_2
from data_to_agg_json
group by key
key | avg_time_1 | avg_time_2
-----+----------------------------------------------------------------------+----------------------------------------------------------------------
1 | [{"avg_time": 10, "category": 10}, {"avg_time": 30, "category": 20}] | [{"avg_time": 20, "category": 10}, {"avg_time": 40, "category": 20}]
2 | [{"avg_time": 10, "category": 10}, {"avg_time": 60, "category": 20}] | [{"avg_time": 50, "category": 10}, {"avg_time": 70, "category": 20}]
(2 rows)
Db<>Fiddle.
Related
that's my dataset:
[
{_id: "628d54495995f1e6589675b3", age: 52, sex: 1, cp: 0, trestbps: 125,
chol: 212, fbs: 0, restecg: "1", thalach: 168, exang: "0", oldpeak: 1},
{_id: "628d54495995f1e6589675b4", age: 53, sex: 1, cp: 1, trestbps: 140,
chol: 203, fbs: 1, restecg: "0", thalach: 155, exang: "1", oldpeak: 3.1},
]
... and so on
I want to group for 2 classes of age (under 40 (included) and over 40 (not included)) and for each class to determine which value of cp is the most frequent.
cp can have four different values: 0, 1, 2, 3
I thought bucket could be the right operator, but I can't get it to work
Thanks for helping
{
"_id": ObjectId("5efb3d1a9ed984485c1a0d25"),
"monthlyVideoStatistics": [
{
"likes": 60,
"_id": ObjectId("5efc5aa1f232f87f944501e1"),
"startDateRange": ISODate("2020-03-01T00:00:00.000Z"),
"endDateRange": ISODate("2020-03-31T23:59:59.999Z"),
"month": 3,
"year": 2020
},
{
"likes": 80,
"_id": ObjectId("5efc5a7eaeded6697c688049"),
"startDateRange": ISODate("2020-04-01T00:00:00.000Z"),
"endDateRange": ISODate("2020-04-30T23:59:59.999Z"),
"month": 4,
"year": 2020
},
{
"likes": 75,
"_id": ObjectId("5efcac3c6f91307578019717"),
"startDateRange": ISODate("2020-05-01T00:00:00.000Z"),
"endDateRange": ISODate("2020-05-31T23:59:59.999Z"),
"month": 5,
"year": 2020
}
]
}
I am having a JSON object like this.
Here I need to compare the likes in month 4(April) were greater or less than in month 3(march)
Is it possible to get the particular array object id or like greater true or false something like that?
here a month, likes are referred to as the field name.
so if need to compare likes in month 3 with month 4, if it will be greater then return the document data or some value like greater
Dears,
I really am not able to achieve following result:
{"2002":[1,2,3...]},
{"2003":[1,2,3,4...]},
...
I have following data (short example below):
{ "Year": "28-01-2020", "numbers": [10, 12, 20, 32, 35, 37] },
{ "Year": "03-10-2019", "numbers": [1, 6, 16, 19, 20, 30] },
{ "Year": "11-01-2018", "numbers": [14, 21, 25, 27, 30, 39] },
{ "Year": "11-08-2015", "numbers": [8, 16, 17, 18, 38, 46] },
I managed to use lodash _.groupBy to achieve following mid result:
[{…}]
0:
2000: Array(4)
0:
Year: "2000"
numbers: (7) [empty, 6, 25, 27, 37, 48, 49]
Year: "2000"
numbers: (7) [empty, 7, 12, 19, 30, 45, 49]
2: {Year: "2000", numbers: Array(7)}
2001: Array(104)
[0 … 99]
[100 … 103]
100: {Year: "2001", numbers: Array(7)}
101: {Year: "2001", numbers: Array(7)}
[0 … 99]
0: {Year: "2002", numbers: Array(7)}
1: {Year: "2002", numbers: Array(7)}
..
but i would like to have one object per year, with all numbers that appeared in sub arrays in this year
Could you please help me, i have tired ES6 map and for in loops, but non give me the proper result
Thank you in advance
This worked for me:
const slicedYear = results.map(elem => (elem = { Year: elem.Year.slice(elem.Year.length - 4), numbers: elem.numbers }));
const groupedResults = _.groupBy(slicedYear, 'Year');
for (let key in groupedResults) {
const selectedNumbers = { [key]: [groupedResults[key].map(elem => elem.numbers)].flat(2) };
}
I have tried to unnest the JSON array with the function json_array_elements() and tried to count the elements of the array using json_array_length(field_name) not being successful. I am using PostgreSQL 9.4.5.
I was looking to query the result for the element "name" this is the data held on the json type array field crew:
[
{
"workHours": "9",
"workers": "50",
"checker_rate": 100,
"rate": 150,
"name": "Ramona",
"last": null,
"boxRate": 2,
"checkTraining": false,
"editing": true,
"ix": 0,
"breakPay": 3.0833333333333335,
"trainingPay": 0
},
{
"workHours": "4",
"workers": "50",
"checker_rate": 120,
"rate": 160,
"name": "Ramon",
"last": "Rosas",
"boxRate": 2,
"checkTraining": false,
"editing": false,
"id": 1,
"breakPay": 1.5416666666666667,
"trainingPay": 0
}
]
Your problem stems from the incorrect use of the type json[]. A json array is a single json object and its type is json, not json[]. Example:
create table test (id int, crew json);
insert into test values
(1, '
[
{
"workHours": "9",
"workers": "50",
"checker_rate": 100,
"rate": 150,
"name": "Ramona",
"last": null,
"boxRate": 2,
"checkTraining": false,
"editing": true,
"ix": 0,
"breakPay": 3.0833333333333335,
"trainingPay": 0
},
{
"workHours": "4",
"workers": "50",
"checker_rate": 120,
"rate": 160,
"name": "Ramon",
"last": "Rosas",
"boxRate": 2,
"checkTraining": false,
"editing": false,
"id": 1,
"breakPay": 1.5416666666666667,
"trainingPay": 0
}
]');
The function json_array_elements() works as expected:
select id, elem->'name' as name
from test
cross join json_array_elements(crew) elem;
id | name
----+----------
1 | "Ramona"
1 | "Ramon"
(2 rows)
One of the queries (or both) should work well with json[]:
select id, elem->'name' as name
from test
cross join json_array_elements(crew[1]) elem;
select id, elem->'name' as name
from test
cross join unnest(crew)
cross join json_array_elements(unnest) elem;
{
actName: null,
applicable: {
applicable: [ 5, 4, 1 ]
},
status: 1,
id: 2
}
{
actName: null,
applicable: {
applicable: [ 3, 2 ]
},
status: 1,
id: 1
}
Is that possible to find value in array, like if i search integer value 2 in applicable array return one row with id 1.
with t(j) as (values
('{
"actName": null,
"applicable": {
"applicable": [ 5, 4, 1 ]
},
"status": 1,
"id": 2
}'::jsonb),
('{
"actName": null,
"applicable": {
"applicable": [ 3, 2 ]
},
"status": 1,
"id": 1
}')
)
select j ->> 'id' as id
from t
where exists (
select 1
from jsonb_array_elements_text(j -> 'applicable' -> 'applicable') s(i)
where i = '2'
)
;
id
----
1
With JSONB's #> you can query for any element by following the structure of your document, for example:
WITH data(d) AS (VALUES
('{
"actName": null,
"applicable": {
"applicable": [ 5, 4, 1 ]
},
"status": 1,
"id": 2
}'::JSONB),
('{
"actName": null,
"applicable": {
"applicable": [ 3, 2 ]
},
"status": 1,
"id": 1
}')
)
SELECT d ->> 'id' AS id
FROM data
WHERE d #> '{"applicable":{"applicable":[1]}}';
Assumption here is that you have jsonb with array of json docs in your database, query may be the following:
WITH test_data AS (
SELECT '[{
"actName": "null",
"applicable": {
"applicable": [5,4,1]
},
"status":1,
"id":2
},
{
"actName": "null",
"applicable": {
"applicable": [3,2]
},
"status": 1,
"id": 1
}]'::JSONB AS jsonb_value
)
SELECT
jsonb_doc->>'id' AS id,
jsonb_doc->'applicable' #>'{applicable}' AS appl_array_result
FROM
test_data td,
jsonb_array_elements(td.jsonb_value) AS jsonb_doc
WHERE (jsonb_doc->'applicable' #>'{applicable}') #> '2'::JSONB;
Output:
id | appl_array_result
----+-------------------
1 | [3, 2]
(1 row)