How to Search for row with Json Array Value as a condition in mysql 5.7 - mysql

I have a table,
t_offices
id name offices
2 london {"officeIds": [1, 33, 13, 1789]}
3 bangkok {"officeIds": [2, 3, 40, 19]}
I can get the array in the json body using
select JSON_EXTRACT(p.`offices`, '$.officeIds[*]') from t_offices p
It leads to
[1, 33, 13, 1789]
[2, 3, 40, 19]
But Now, How to search with a condition that it would have value 33.
i.e
2 London {"officeIds": [1, 33, 13, 1789]}
basically, get the row where a value is inside that json array.

You can try with this query:
SELECT * FROM t_offices WHERE JSON_CONTAINS(offices, '33', '$.officeIds');
OR
SELECT * FROM t_offices WHERE JSON_CONTAINS(offices->'$.officeIds', '33');

Related

Musician wants to know the programming term for this function

My apologies that I don't use always use programming terms in my description - I am a musician who has only dabbled in programming.
Suppose I have a list of numbers named a:
a = (0, 2, 4, 5, 7, 9, 11, 12)
and from the list a the following list of numbers is randomly generated to create list b:
b = (4, 7, 5, 7, 9, 11, 9)
Then I want to have "transpositions" (this is a musical term) of list b, such as those shown in lists c, d, and e:
c = (5, 9, 7, 9, 11, 12, 11) or d = (2, 5, 4, 5, 7, 9, 11, 9) or e = (0, 4, 2, 4, 5, 7, 9, 7)
What do you call this "transposition" of this list of numbers in programming terms, and what bit of programming code would accomplish this task? My best guess is that it has to do with the indexing of the numbers in list a, and when list b is created the indexes of list b are put in a list such as list f:
f = (2, 4, 3, 4, 5, 6, 5)
so that the "transposition" is accomplished by adding or subtracting a specific number from each number in list f. For example, the numbers in list c are generated by adding 1 to each of the numbers in list f:
(3, 5, 4, 5, 6, 7, 6)
the numbers in list d are generated by subtracting 1 to each of the numbers in list f:
(1, 3, 2, 3, 4, 5, 4)
and the numbers in list e are generated by subtracting 2 to each of the index numbers taken from list f:
(0, 2, 1, 2, 3, 4, 3)
Or if anyone has a better idea, please let me know.
An operation like "add 1 to every member of this list, to generate a new list" is usually called a map.

MySQL merge json field with new data while removing duplicates, where the json values are simple scalar values

Suppose that I have a MySQL table with a JSON field that contains only numbers, like this (note: using MySQL 8):
CREATE TABLE my_table (
id int,
some_field json
);
Sample data:
id: 1
some_field: [1, 2, 3, 4, 5]
id: 2
some_field: [3, 6, 7]
id: 3
some_field: null
I would like to merge another array of data with the existing values of some_field, while removing duplicates. I was hoping that this might work, but it didn't:
update my_table set some_field = JSON_MERGE([1, 2, 3], some_field)
The result of this would be:
id: 1
some_field: [1, 2, 3, 4, 5]
id: 2
some_field: [1, 2, 3, 6, 7]
id: 3
some_field: [1, 2, 3]
Considering you have 3 records in your table and you want to merge 1 and 2 as mentioned in your example.
I hope JavaScript is suitable to follow through for you.
// Get both the records
const records = db.execute(“SELECT id, some_field FROM my_table WHERE id=1 OR id=2”);
// You get both the rows.
// Merging row1, you can either use the Set data structure if you’re dealing with numbers like your example, or you could loop using a map and use the spread operator if using JSON. Since your object is an array, I’ll just be explaining to merge 2 arrays.
records[0].some_field = Array.from(new Set(records[0].some_field + record[1].some_field))
// Same for second record.
records[1].some_field = Array.from(new Set(records[0].some_field + record[1].some_field))
// Now update both the records in the database one by one.

Query for array elements inside Jsonb type postgreSQL

I have a psql table where one of the jsonb data is extracted over it.
{
"SrcRcs": [4, 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 158],
"reason": "",
"result": "Success",
"InitTech": 1
}
This column is named Data and is of type jsonb.
I am extracting the SrcRcs data from the jsonb:
select Data->'SrcRcs' from table_name;
Output:
[4, 0, 1, 2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 158]
But which is in unsorted order as from the jsonb.
I want it in the sorted order like this:
[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,158]
Can someone please help me out?
I have tried the psql sort() but wasn't able to achieve the desired result.
You need to unnest the array elements and then aggregate them back in a sorted way:
SELECT (select jsonb_agg(i::int order by i::int)
from jsonb_array_elements(data -> 'SrcRcs') as t(i))
from the_table
If you want, you can create a function for this to make the SQL queries easier.

SQL: value higher than percentage of population of values

I wish to calculate the value which is higher than a percentage of the population of values, this per group.
Suppose I have:
CREATE TABLE project
(
id int,
event int,
val int
);
INSERT INTO project(id,event,val)
VALUES
(1, 11, 43),
(1, 12, 19),
(1, 13, 19),
(1, 14, 53),
(1, 15, 45),
(1, 16, 35),
(2, 21, 22),
(2, 22, 30),
(2, 23, 25),
(2, 24, 28);
I now want to calculate for each id what is the val that will be for example higher than 5%, or 30% of the val for that id.
For example, for id=1, we have the following values: 43, 19, 19, 53, 45, 35.
So the contingency table would look like this:
19 35 43 45 53
2 1 1 1 1
and the val=20 (higher than 19) would be chosen to be higher than 5% (actuall 2 out of 6) of the rows.
The contengency table for id 2 is:
22 25 28 30
1 1 1 1
My expected out is:
id val_5p_coverage val_50p_coverage
1 20 36
2 23 26
val_5p_coverage is the value val needed to be above at least 5% of val in the id.
val_50p_coverage is the value val needed to be above at least 50% of val in the id.
How can I calculate this with SQL ?
I managed to do it in HiveQL (for Hadoop) as follows:
create table prep as
select *,
CUME_DIST() OVER(PARTITION BY id ORDER BY val ASC) as proportion_val_equal_or_lower
from project
SELECT id,
MIN(IF(proportion_val_equal_or_lower>=0.05, val, NULL)) AS val_5p_coverage,
MIN(IF(proportion_val_equal_or_lower>=0.50, val, NULL)) AS val_50p_coverage
FROM prep
GROUP BY id
Although this is not MySQL nor SQL per se, it might help to do it in MySQL or SQL.

Blank parameter with multivalue parameter returns nothing

I have three parameter (#person_id, #Person_name, #Supervisor_name), all have Allow Multiple Values and Allow blank value property enabled.
Columns of the report are Person_id, Person_name, Supervisor_name, Claims_done, average_claims_perday created with dataset table with same columns.
The dataset which return the data has filter in query:
where #person_id in (#person_id)
or [PersonName] in (#Person_name)
or Supervisor_name in (#supervisor_name)
The requirement is out of three parameter, if any of the parameter is blank, then query should gives the result based on the parameters that are selected with multivalued.
For Example: dataset creates the following result.
11, abc, john, 12, 3
22, def, john, 345, 9
33, ghi, bryan, 89, 7
44, jkl, bryan, 45, 6
55, mno, bryan, 60, 7
If I select the parmeter #Person_name = 'mno' and #Supervisor_name = 'John' and kept #person_id blank then it should give the result:
11, abc, john, 12, 3
22, def, john, 345, 9
55, mno, bryan, 60, 7
If I select #person_id = 11, 44 and #Supervisorname = 'John', and left the #Person_name blank, then it should give the result:
11, abc, john, 12, 3
22, def, john, 345, 9
44, jkl, bryan, 45, 6
When I keep any of the parameter blank, the report doesnt shows anything, If I select at least one value for all parameters, it gives perfect result.
Any help is appreciated.
If I understand correctly, your requirements for handling parameters can be rephrased as: If a parameter is set, then filter on it; otherwise don't filter on it.
If that is correct, change the where clause to something like this:
WHERE (Person_id in (#person_id) OR #person_id = '')
AND (PersonName in (#Person_name) OR #Person_name = '')
AND (Supervisor_name in (#supervisor_name) OR #supervisor_name = '')
This means each parameter has to be either satisfied, or has to be blank.