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

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
}
]

Related

measure of how similar an object is to its own cluster or to other clusters

I have a set of samples. These samples are partitioned based on their values into several clusters (3 clusters).
df = pd.DataFrame({'samples': ['A', 'B', 'C', 'D', 'E'],
'values': [[5, 0, 2, 2],[1, 6, 0, 2],[7, 2, 0, 0],[3, 6, 0, 0],[7, 0, 0, 2]],
'cluster': [1, 0, 2, 0, 1]})
df
output:
samples values cluster
0 A [5, 0, 2, 2] 1
1 B [1, 6, 0, 2] 0
2 C [7, 2, 0, 0] 2
3 D [3, 6, 0, 0] 0
4 E [7, 0, 0, 2] 1
Assuming after a round, the value of sample (B) has changed from [1, 6, 0, 2] to [7, 1, 0, 2]
How could I measure whether sample (B) still belongs to its own cluster (0) or should move to other clusters?
samples values cluster
0 A [5, 0, 2, 2] 1
1 B [7, 1, 0, 2] ?
2 C [7, 2, 0, 0] 2
3 D [3, 6, 0, 0] 0
4 E [7, 0, 0, 2] 1
I could use silhouette index, but I looking for different methods such as math formulas, or other techniques.

How do I get min of jsonb array

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

VS Code changes indentation on copy/paste

I use Python. According to the PEP 8 Style Guide, both of these are acceptable -
my_list = [
1, 2, 3,
4, 5, 6,
]
my_list = [
1, 2, 3,
4, 5, 6,
]
I prefer the former. However, if I copy and paste a block like this, VS Code changes the indentation like this -
my_list = [
1, 2, 3,
4, 5, 6,
]
my_list = [
1, 2, 3,
4, 5, 6,
]
Is there a setting to change this behaviour?

reactivex repeated skip between

Suppose I have the following stream of data:
1, 2, 3, a, 5, 6, b, 7, 8, a, 10, 11, b, 12, 13, ...
I want to filter everything between 'a' and 'b' (inclusive) no matter how many times they appear. So the result of the above would be:
1, 2, 3, 7, 8, 12, 13, ...
How can I do this with ReactiveX?
Use scan with initial value b to turn
1, 2, 3, a, 5, 6, b, 7, 8, a, 10, 11, b, 12, 13, ...
into
b, 1, 2, 3, a, a, a, b, 7, 8, a, a, a, b, 12, 13, ...
and then filter out a and b to get
1, 2, 3, 7, 8, 12, 13, ...
In pseudo code
values.scan('b', (s, v) -> if (v == 'a' || v == 'b' || s != 'a') v else s).
filter(v -> v != 'a' && v != 'b');
OK. I'm posting this in case anyone else needs an answer to it. A slightly different setup than I described above just to make it easier to understand.
List<String> values = new List<string>()
{
"1", "2", "3",
"a", "5", "6", "b",
"8", "9", "10", "11",
"a", "13", "14", "b",
"16", "17", "18", "19",
"a", "21", "22", "b",
"24"
};
var aa =
// Create an array of CSV strings split on the terminal sigil value
String.Join(",", values.ToArray())
.Split(new String[] { "b," }, StringSplitOptions.None)
// Create the observable from this array of CSV strings
.ToObservable()
// Now create an Observable from each element, splitting it up again
// It is no longer a CSV string but the original elements up to each terminal value
.Select(s => s.Split(',').ToObservable()
// From each value in each observable take those elements
// up to the initial sigil
.TakeWhile(s1 => !s1.Equals("a")))
// Concat the output of each individual Observable - in order
// SelectMany won't work here since it could interleave the
// output of the different Observables created above.
.Concat();
aa.Subscribe(s => Console.WriteLine(s));
This prints out:
1
2
3
8
9
10
11
16
17
18
19
24
It is a bit more convoluted than I wanted but it works.
Edit 6/3/17:
I actually found a cleaner solution for my case.
List<String> values = new List<string>()
{
"1", "2", "3",
"a", "5", "6", "b",
"8", "9", "10", "11",
"a", "13", "14", "b",
"16", "17", "18", "19",
"a", "21", "22", "b",
"24"
};
string lazyABPattern = #"a.*?b";
Regex abRegex = new Regex(lazyABPattern);
var bb = values.ToObservable()
.Aggregate((s1, s2) => s1 + "," + s2)
.Select(s => abRegex.Replace(s, ""))
.Select(s => s.Split(',').ToObservable())
.Concat();
bb.Subscribe(s => Console.WriteLine(s));
The code is simpler which makes it easier to follow (even though it uses regexes).
The problem here is that it still isn't really a general solution to the problem of removing 'repeated regions' of a data stream. It relies on converting the stream to a single string, operating on the string, then converting it back to some other form. If anyone has any ideas on how to approach this in a general way I would love to hear about it.

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.