How do I get min of jsonb array - postgresql

I have this
[{"bed": 0, "bath": 1, "price": [1768]}, {"bed": 0, "bath": 1, "price": [1824, 1824, 1828, 1869, 1869]}, {"bed": 1, "bath": 1, "price": [2085, 2247, 2247]}, {"bed": 1, "bath": 1, "price": [2144]}, {"bed": 1, "bath": 1, "price": [2223, 2177]}, {"bed": 1, "bath": 1, "price": [2205]}, {"bed": 1, "bath": 1, "price": [2237]}, {"bed": 2, "bath": 2, "price": [2982, 2982, 2982, 3017, 3162]}, {"bed": 2, "bath": 2, "price": [3297]}]
I want to get price min value where bed >0 and price >2000
I tried this
SELECT id,((jsonb_array_elements(p.bedBathPrice)->'bed')) FROM
properties p where p.id = 2
So can I do something below like bed >0 and price >2000 and then get min price
but what if I need to add p.bedbathprice in that value
with the_original_table ( doc_data) as
(
values
( p.bedbathprice::jsonb)
),
normalized_data as
(
select
(j ->> 'bath')::integer as bath,
(j ->> 'bed')::integer as bed,
p::numeric as price
from the_original_table
cross join lateral jsonb_array_elements(doc_data) as j
cross join lateral jsonb_array_elements(j -> 'price') as p
)
select p.id ,(select min(nd_price) as min_price from (select (price) as nd_price from normalized_data nd where nd.bed>=2 and nd.price>2085)as nd) from properties p where p.parent_id is not null and p.id=3
and (exists(select 1 from normalized_data where bed>=2 and price>2085));
like this??? it says from clouse can't use p.bedbthprice in value

with j as (select jsonb_array_elements('[
{"bed": 0, "bath": 1, "price": [1768]},
{"bed": 0, "bath": 1, "price": [1824, 1824, 1828, 1869, 1869]},
{"bed": 1, "bath": 1, "price": [2085, 2247, 2247]},
{"bed": 1, "bath": 1, "price": [2144]},
{"bed": 1, "bath": 1, "price": [2223, 2177]},
{"bed": 1, "bath": 1, "price": [2205]},
{"bed": 1, "bath": 1, "price": [2237]},
{"bed": 2, "bath": 2, "price": [2982, 2982, 2982, 3017, 3162]},
{"bed": 2, "bath": 2, "price": [3297]}
]'::jsonb) v)
select j.v->>'bed' bed, (select min(value) from
jsonb_array_elements_text(j.v->'price')) minimum from j
where (j.v->>'bed')::integer >0;
output
bed | minimum
-----+---------
1 | 2085
1 | 2144
1 | 2177
1 | 2205
1 | 2237
2 | 2982
2 | 3297

Related

I can't join a table on a jsonb value - Postgresql [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 months ago.
This post was edited and submitted for review 2 months ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I can't quitely understand what's others solution in others question, I hope you guys can elaborate more
I have this query and it works like I want it to.
SELECT
stu.fname || ' ' || stu.lname AS "name",
sch."name" AS school_name,
stu.custom_school_name,
tr.created_at,
tr.weights::JSON->0->>'ctgr' AS first_char,
tr.weights::JSON->1->>'ctgr' AS second_char,
tr.weights::JSON->2->>'ctgr' AS third_char
FROM
tmb_result tr
JOIN students stu ON stu."id" = tr.student_id
JOIN schools sch ON sch."id" = stu.school_id
WHERE
tr.created_at BETWEEN '2022-11-24 00:00:00+07' AND '2022-11-30 24:00:00+07';
+------------+-------------+------------+
| first_char | second_char | third_char |
+------------+-------------+------------+
| R | I | A |
| R | I | A |
| R | I | A |
+------------+-------------+------------+
but my senior wants me to change the first, second, and third_char data to this label_long data from this table based on this table code
how can I join them? this is the jsonb data from tr.weights
[
{
"ctgr": "R",
"sw_ct": 3,
"ksw_ct": 1,
"ssw_ct": 3,
"tsw_ct": 0,
"ctgr_ord": 1,
"total_wght": 23
},
{
"ctgr": "I",
"sw_ct": 4,
"ksw_ct": 1,
"ssw_ct": 2,
"tsw_ct": 0,
"ctgr_ord": 2,
"total_wght": 22
},
{
"ctgr": "A",
"sw_ct": 2,
"ksw_ct": 2,
"ssw_ct": 3,
"tsw_ct": 0,
"ctgr_ord": 3,
"total_wght": 22
},
{
"ctgr": "S",
"sw_ct": 3,
"ksw_ct": 0,
"ssw_ct": 2,
"tsw_ct": 2,
"ctgr_ord": 4,
"total_wght": 19
},
{
"ctgr": "E",
"sw_ct": 5,
"ksw_ct": 1,
"ssw_ct": 1,
"tsw_ct": 0,
"ctgr_ord": 5,
"total_wght": 21
},
{
"ctgr": "C",
"sw_ct": 3,
"ksw_ct": 3,
"ssw_ct": 1,
"tsw_ct": 0,
"ctgr_ord": 6,
"total_wght": 19
}
]

Logical indexing with two conditions

I have a rankingMat which contains the rankings of equity tickers, where every column represents one ticker and every row represents a point in time. The ranking has been performed in a descending manner, i.e. a 1 in rankingMat identifies the highest rank for that time period (read: row). Ticker/column 4 represents a cash security. This will be important further down the road.
Now, I would like to identify in which equity tickers I am supposed to invest. There exists two conditions:
I only invest in tickers that have a rank less or equal to 3.
In addition, I only invest in tickers that are ranked higher or equal compared to cash (ticker/column 4).
I'm able to get a result that fulfills the first condition:
rankingMat = ...
[NaN, NaN, NaN, NaN, NaN, NaN; ...
1, 5, 2, 3, 6, 4; ...
4, 5, 2, 3, 6, 1; ...
4, 1, 2, 5, 6, 3; ...
6, 4, 5, 2, 1, 3; ...
2, 3, 4, 6, 1, 5; ...
3, 6, 4, 1, 2, 5; ...
2, 5, 6, 1, 4, 3];
portfolio = rankingMat <= 3;
The result looks like this:
portfolio = ...
[0, 0, 0, 0, 0, 0; ...
1, 0, 1, 1, 0, 0; ...
0, 0, 1, 1, 0, 1; ...
0, 1, 1, 0, 0, 1; ...
0, 0, 0, 1, 1, 1; ...
1, 1, 0, 0, 1, 0; ...
1, 0, 0, 1, 1, 0; ...
1, 0, 0, 1, 0, 1]
My problem is condition 2. In every row, I need to compare not only if the integer is less or equal to 3, I also need to make sure that it is less than the integer in column 4 in that particular row. I am looking for a solution that avoids a for-loop. I guess it is possible with indexing. So, any hint is highly appreciated.
The final result should look like this:
portfolio = ...
[0, 0, 0, 0, 0, 0; ...
1, 0, 1, 1, 0, 0; ...
0, 0, 1, 1, 0, 1; ...
0, 1, 1, 0, 0, 1; ...
0, 0, 0, 1, 1, 0; ...
1, 1, 0, 0, 1, 0; ...
0, 0, 0, 1, 0, 0; ...
0, 0, 0, 1, 0, 0]
% Prior to R2016b
portfolio = rankingMat <= 3 & ...
bsxfun(#lt, rankingMat, rankingMat(:,4));
% On or after R2016b
portfolio = rankingMat <= 3 & ...
rankingMat < rankingMat(:,4);

Combine array from embedded document based on condition in MongoDB

I have a collection of student details like below:
{
"Student_id": 1,
"StudentName": "ABC",
"TestDetails": [{
"SubtestName":"Reading", "TestSeq":1, "SubTestDetails":1,
"Scores":[{"ScoreType":"YY","ScoreValue":"100"},{"ScoreType":"XX","ScoreValue":"100"},
{"ScoreType": "ZZ","ScoreValue":"100"}]}]
,
"TestDetails": [{
"SubtestName":"Writing", "TestSeq":1, "SubTestDetails":2,
"Scores":[{"ScoreType":"YY","ScoreValue":"200"},{"ScoreType":"XX","ScoreValue":"200"},
{"ScoreType": "ZZ","ScoreValue":"200"}]}]
,
"TestDetails": [{
"SubtestName":"Listning", "TestSeq":2, "SubTestDetails":3,
"Scores":[{"ScoreType":"YY","ScoreValue":"300"},{"ScoreType":"XX","ScoreValue":"300"},
{"ScoreType": "ZZ","ScoreValue":"300"}]}]
,
"TestDetails": [{
"SubtestName":"Speaking", "TestSeq":2, "SubTestDetails":4,
"Scores":[{"ScoreType":"YY","ScoreValue":"400"},{"ScoreType":"XX","ScoreValue":"400"},
{"ScoreType": "ZZ","ScoreValue":"400"}]}]
,
"TestDetails": [{
"SubtestName":"Smartness", "TestSeq":3, "SubTestDetails":5,
"Scores":[{"ScoreType":"YY","ScoreValue":"500"},{"ScoreType":"XX","ScoreValue":"500"},
{"ScoreType": "ZZ","ScoreValue":"500"}]}]
},
{
"Student_id": 2,
"StudentName": "XYZ",
"TestDetails": [{
"SubtestName":"Smartness", "TestSeq":1, "SubTestDetails":1,
"Scores":[{"ScoreType":"YY","ScoreValue":"100"},{"ScoreType":"XX","ScoreValue":"100"},
{"ScoreType": "ZZ","ScoreValue":"100"}]}]
,
"TestDetails": [{
"SubtestName":"Writing", "TestSeq":1, "SubTestDetails":2,
"Scores":[{"ScoreType":"YY","ScoreValue":"200"},{"ScoreType":"XX","ScoreValue":"200"},
{"ScoreType": "ZZ","ScoreValue":"200"}]}]
,
"TestDetails": [{
"SubtestName":"Listning", "TestSeq":2, "SubTestDetails":3,
"Scores":[{"ScoreType":"YY","ScoreValue":"300"},{"ScoreType":"XX","ScoreValue":"300"},
{"ScoreType": "ZZ","ScoreValue":"300"}]}]
,
"TestDetails": [{
"SubtestName":"Speaking", "TestSeq":2, "SubTestDetails":4,
"Scores":[{"ScoreType":"YY","ScoreValue":"400"},{"ScoreType":"XX","ScoreValue":"400"},
{"ScoreType": "ZZ","ScoreValue":"400"}]}]
,
"TestDetails": [{
"SubtestName":"Reading", "TestSeq":3, "SubTestDetails":5,
"Scores":[{"ScoreType":"YY","ScoreValue":"100"},{"ScoreType":"XX","ScoreValue":"100"},
{"ScoreType": "ZZ","ScoreValue":"1000"}]}]
},
.
.
.
)
How can I create aggregate query to generate document like below:
{Student:1, "TestSeq" : 1, [{Subtest_name: Reading},{Subtest_name: Writing}]},
{Student:1,"TestSeq" : 2, [{Subtest_name: Listning},{Subtest_name: Speaking}]},
{Student:1, "TestSeq" : 3, [{Subtest_name: Smartness}]},
{Student:2, "TestSeq" : 1, [{Subtest_name: Smartness},{Subtest_name: Writing}]},
{Student:2, "TestSeq" : 2, [{Subtest_name: Listning},{Subtest_name: Speaking}]},
{Student:2, "TestSeq" : 3, [{Subtest_name: Reading}]},
{Student:3, "TestSeq" : 1, [{Subtest_name: Subtest1},{Subtest_name: Subtest2}]},
{Student:3, "TestSeq" : 2, [{Subtest_name: Subtest3},{Subtest_name: Subtest4}]},
{Student:3, "TestSeq" : 3, [{Subtest_name: Subtest5}]}
Logic is to combine/group Subtest name based on TestSeq values. For example Subtest names are combined for TestSeq = 1, for value 2 it's in 2nd row and 3 for last Subtest name for each student.
How can I implement that?
I have tried as below -
db.students.aggregate([
{$unwind: "$SubtestAttribs"},
{ $project: { student_name: 1, student_id : 1,
print_ready : "$SubtestAttribs.TestSeq",
Subtest_names :$SubtestAttribs.SubtestName" } } ])
But I am unable to form array based on condition. Above snippet giving data for each test seq. But how to combine two sub test name based on test seq?
Note: I'm making a couple assumptions because your question has some illegal JSON in it. Let me know if I guessed wrong. Also, I'm not on a computer with Mongo right now, so I might have some syntax issues.
db.students.aggregate([
{ $unwind: "$TestDetails" },
{
$group:{
_id: { Student: "$Student_id", TestSeq: "$TestDetails.TestSeq},
Subtest_names: { $addToSet: "$TestDetails.Subtestname" }
}
},
{
$project:{
Student: "$_id.Student",
TestSeq: "$_id.TestSeq,
Subtest_names: "$Subtest_names"
}
}
])

Get values from an RDD

I created an RDD wtih the following format using Scala :
Array[(String, (Array[String], Array[String]))]
How can I get the list of the Array[1] from this RDD?
The data for the first data line is:
// Array[(String, (Array[String], Array[String]))]
Array(
(
966515171418,
(
Array(4579848447, 4579848453, 2015-07-29 03:27:28, 44, 1, 1, 966515171418, 966515183263, 420500052424347, 0, 52643, 9, 5067, 5084, 2, 1, 0, 0),
Array(4579866236, 4579866226, 2015-07-29 04:16:22, 37, 1, 1, 966515171418, 966515183264, 420500052424347, 0, 3083, 9, 5072, 5084, 2, 1, 0, 0)
)
)
)
Assuming you have something like this (just paste into a spark-shell):
val a = Array(
("966515171418",
(Array("4579848447", "4579848453", "2015-07-29 03:27:28", "44", "1", "1", "966515171418", "966515183263", "420500052424347", "0", "52643", "9", "5067", "5084", "2", "1", "0", "0"),
Array("4579866236", "4579866226", "2015-07-29 04:16:22", "37", "1", "1", "966515171418", "966515183264", "420500052424347", "0", "3083", "9", "5072", "5084", "2", "1", "0", "0")))
)
val rdd = sc.makeRDD(a)
then you get the first array using
scala> rdd.first._2._1
res9: Array[String] = Array(4579848447, 4579848453, 2015-07-29 03:27:28, 44, 1, 1, 966515171418, 966515183263, 420500052424347, 0, 52643, 9, 5067, 5084, 2, 1, 0, 0)
which means the first row (which is a Tuple2), then the 2nd element of the tuple (which is again a Tuple2), then the 1st element.
Using pattern matching
scala> rdd.first match { case (_, (array1, _)) => array1 }
res30: Array[String] = Array(4579848447, 4579848453, 2015-07-29 03:27:28, 44, 1, 1, 966515171418, 966515183263, 420500052424347, 0, 52643, 9, 5067, 5084, 2, 1, 0, 0)
If you want to get it of all rows, just use map():
scala> rdd.map(_._2._1).collect()
which puts the results of all rows into an array.
Another option is to use pattern matching in map():
scala> rdd.map { case (_, (array1, _)) => array1 }.collect()

Facebook Graph empty data for likes, tags etc

1) I'm wondering why the Facebook Graph gives empty results for the following objects:
(Please keep in mind I have a token that has the correct permissions, moreover, extended permissions to access these objects).
tagged - https://graph.facebook.com/me/tagged or /{userid}/tagged
likes
activities
accounts
which produces:
{"data": []}
2) Did Facebook strip our permissions from accessing our own data?
3) Which objects cannot be accessed?
4) Here are my extended /permissions
<pre>
{
"data": [
{
"installed": 1,
"status_update": 1,
"publish_checkins": 1,
"photo_upload": 1,
"video_upload": 1,
"sms": 1,
"email": 1,
"create_event": 1,
"create_note": 1,
"export_stream": 1,
"share_item": 1,
"rsvp_event": 1,
"read_stream": 1,
"publish_stream": 1,
"read_mailbox": 1,
"ads_management": 1,
"read_friendlists": 1,
"manage_friendlists": 1,
"xmpp_login": 1,
"read_insights": 1,
"read_requests": 1,
"manage_notifications": 1,
"manage_pages": 1,
"publish_actions": 1,
"user_birthday": 1,
"user_religion_politics": 1,
"user_relationships": 1,
"user_relationship_details": 1,
"user_hometown": 1,
"user_location": 1,
"user_likes": 1,
"user_activities": 1,
"user_interests": 1,
"user_education_history": 1,
"user_work_history": 1,
"user_online_presence": 1,
"user_website": 1,
"user_groups": 1,
"user_events": 1,
"user_photos": 1,
"user_videos": 1,
"user_photo_video_tags": 1,
"user_notes": 1,
"user_checkins": 1,
"user_questions": 1,
"user_about_me": 1,
"user_status": 1,
"user_games_activity": 1,
"user_subscriptions": 1,
"friends_birthday": 1,
"friends_religion_politics": 1,
"friends_relationships": 1,
"friends_relationship_details": 1,
"friends_hometown": 1,
"friends_location": 1,
"friends_likes": 1,
"friends_activities": 1,
"friends_interests": 1,
"friends_education_history": 1,
"friends_work_history": 1,
"friends_online_presence": 1,
"friends_website": 1,
"friends_groups": 1,
"friends_events": 1,
"friends_photos": 1,
"friends_videos": 1,
"friends_photo_video_tags": 1,
"friends_notes": 1,
"friends_checkins": 1,
"friends_questions": 1,
"friends_about_me": 1,
"friends_status": 1,
"friends_games_activity": 1,
"friends_subscriptions": 1
}
]
}
</pre>
I've checked them, I don't see any tags (but not sure if I have been tagged in the last few days).
The other ones work perfectly for me when tested in the https://developers.facebook.com/tools/explorer. So are you sure your account has likes/activities and more then one account? Since I only see my 'pages' of which I am also owner.