Query for array elements inside Jsonb type postgreSQL - json

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.

Related

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

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');

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.

Can anyone provide me with some insight into why this query is taking a long time to run?

SELECT PatientID as 'PatientIDIX', claims.ClaimID as 'ClaimIDIX', ClaimThroughDate as 'ClaimThroughDateIX', HCPCCode, ProcedureID
FROM `t1`.revenue revenue
JOIN `t1`.patient patient ON revenue.ClaimID = patient.ClaimID
JOIN `t1`.claims claims ON claims.ClaimID = revenue.ClaimID
LEFT JOIN `t1`.procedures procedures ON procedures.ClaimID = revenue.ClaimID
WHERE HCPCCode IN (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)
OR ProcCode IN (11, 22, 33, 44, 55, 66, 77, 88, 99, 1010);
I was wondering if anyone has some insight into why the above query is taking so long. I believe it is because of the left join. Thanks!
I found a few articles that may help you:
http://explainextended.com/2009/08/18/passing-parameters-in-mysql-in-list-vs-temporary-table/
http://www.slideshare.net/techdude/how-to-kill-mysql-performance
MYSQL OR vs IN performance
Depending on your data, indexing and other factors, the simplest solution may be to use a temp table instead of using the IN statement.

Extract the minimum positive value of each row in a matrix?

I have a matrix like this
A =
[0, 0, 0, 1, 3, 7, NA;
0, 0, 3, 5, 7, NA, NA;
0, 2, 3, 4, 5, 6, NA;
0, 0, 4, 5, 6, 7, NA;]
I want to extract the minimum values in each row of the matrix A that is greater than 0 into a vector B:
B = [1;3;2;4]
Any suggestion?
Thank you very much.
A(A<=0)=NA;
B=min(A, [], 2)
As suggested by Matt I'll explain this a little bit. Because you don't want results <=0 I set them to NA. You already have some in your data and the "min" operation will ignore them.
In the second step we search the minimum in the rows (2. dimension).

MySQL order by 0 then largest

I am trying to do a mysql sort that displays 0 first and then by the largest number.
My current mysql statement returns
10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0
But I would like to get this
0, 0, 0, 0, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
Is it possible to build a MySQL query that orders an integer from largest to smallest with 0 at the beginning?
Try this order by statement:
order by val = 0 desc, val desc
The first part is a boolean that evaluates to "1" when the value is 1 and otherwise 0. The second orders the rest of the values in descending order.
you have to use 2 filters
select * from mytable
order by mycolumn=0 desc, mycolumn desc