BigQuery Standard SQL, get max value from json array - sql

I have a BigQuery column which contains STRING values like
col1
[{"a":1,"b":2},{"a":2,"b":3}]
[{"a":3,"b":4},{"a":5,"b":6}]
Now when doing a SELECT for each I want to get just the max. value of "a" in each json array for example here I would want the output of the SELECT on the table to be
2
5
Any ideas please? Thanks!

Use JSON_EXTRACT_ARRAY() to retrieve each array element. Then JSON_EXTRACT_VALUE():
with t as (
select '[{"a":1,"b":2},{"a":2,"b":3}]' as col union all
select '[{"a":3,"b":4},{"a":5,"b":6}]'
)
select t.*,
(select max(json_value(el, '$.a'))
from unnest(JSON_QUERY_ARRAY(col, '$')) el
)
from t;

Related

Convert Array Athena into String

I currently have a JSON output as an array in Athena:
This is the query Im running
WITH dataset AS (SELECT
Items
FROM
(SELECT * FROM (
SELECT
JSON_EXTRACT(message, '$.items') AS Items
FROM kafka.database
)))
select * from dataset
LIMIT 10
And this is the current Ouput
["item0","item1","item2","item3"]
But would like to generate the Ouput from AWS Athena in this way:
"item0,item1,item2,item3"
I have tried to follow this steps from Athena documentation but it's not working:
WITH dataset AS (SELECT
Items
FROM
(SELECT * FROM (
SELECT
array_join(JSON_EXTRACT(message, '$.items'),' ') AS Items
FROM kafka.database
)))
select * from dataset
LIMIT 10
But for example, in this way, I am able to select the first Item in the JSON output.
WITH dataset AS (SELECT
Items
FROM
(SELECT * FROM (
SELECT
json_array_get(JSON_EXTRACT(message, '$.items'),0) AS Items
FROM kafka.database
)))
select * from dataset
LIMIT 10
JSON_EXTRACT does not return an array, it returns value of json type, so directly manipulating it as an array is not supported. One way to handle it is to cast it to array(varchar) and use array_join on it:
-- sample data
WITH dataset (json_arr) AS (
VALUES (json '["item0","item1","item2","item3"]')
)
-- query
select array_join(cast(json_arr as array(varchar)), ', ')
from dataset;
Output:
_col0
item0, item1, item2, item3

How to fin and extract substring BIGQUERY

A have a string column at BigQuery table for example:
name
WW_for_all_feed
EU_param_1_for_all_feed
AU_for_all_full_settings_18+
WW_for_us_param_5_for_us_feed
WW_for_us_param_5_feed
WW_for_all_25+
and also have a list of variables, for example :
param_1_for_all
param_5_for_us
param_5
full_settings
And if string at column "name" contains one of this substrings needs to extract it :
name
param
WW_for_all_feed
None
EU_param_1_for_all_feed
param_1_for_all
AU_for_all_full_settings_18+
full_settings
WW_for_us_param_5_for_us_feed
param_5_for_us
WW_for_us_param_5_feed
param_5
WW_for_all_25+
None
I want to try regexp and replace, but don't know pattern for find substring
Use below
select name, param
from your_table
left join params
on regexp_contains(name, param)
if apply to sample data as in your question
with your_table as (
select 'WW_for_all_feed' name union all
select 'EU_param_1_for_all_feed' union all
select 'AU_for_all_full_settings_18+' union all
select 'WW_for_us_param_5_for_us_feed' union all
select 'WW_for_all_25+'
), params as (
select 'param_1_for_all' param union all
select 'param_5_for_us' union all
select 'full_settings'
)
output is
but I have an another issue (updated question) If one of params is substring for another?
use below then
select name, string_agg(param order by length(param) desc limit 1) param
from your_table
left join params
on regexp_contains(name, param)
group by name
if applied to your updated data sample - output is

How I get a one row of elements from nested JSON in MariaDB?

is there any SOL query in MariaDB to get a single row of JSON from nested JSON array
[
{"connection":[{"f_id":"593435068"}],"connection_id":1},
{"connection":[{"f_id":"1103648403"}],"connection_id":2}
]
I am trying to that:
if I add 1 in SQL query it will give me
{"connection":[{"f_id":"593435068"}],"connection_id":1}
if I add 2 in SQL query it will give me
{"connection":[{"f_id":"1103648403"}],"connection_id":2}
I did this successfully in MYSQL with the help of JSON_TABLE()
as :
SELECT jd.con_name,jd.con_id
FROM your_table,
JSON_TABLE(json_col, "$[*]"
COLUMNS( con_name JSON PATH "$.connection", con_id JSON PATH "$.connection_id" ) ) AS jd
WHERE jd.con_id = 1;
This way i get always accurate row
but I am unable to do this in MariaDB
can anyone please help me
Thanks
You can use JSON_EXTRACT() function :
SELECT JSON_EXTRACT(js, '$[0]'), JSON_EXTRACT(js, '$[1]')
FROM your_table
Demo 1
MariaDB has no JSON_TABLE() support yet.
Assume that you have another table(t2) holding ID values 1 and 2, then consider using :
SELECT JSON_EXTRACT(t1.js, CONCAT('$[', t2.ID - 1, ']')) AS col
FROM t2
CROSS JOIN t1
Demo 2
One option may be to use a combination of functions:
JSON_SEARCH
REGEXP_REPLACE
CONCAT
JSON_EXTRACT
SELECT
JSON_EXTRACT(`jd`.`con_json`, '$.connection') `con_name`,
JSON_EXTRACT(`jd`.`con_json`, '$.connection_id') `con_id`,
`jd`.`con_json`
FROM (
SELECT
JSON_EXTRACT(
`json_col`,
CONCAT('$[',
REGEXP_REPLACE(
JSON_SEARCH(
`json_col`,
'one',
#`id_search`,
NULL,
'$[*].connection_id'
),
'[^0-9]',
''
),
']')
) `con_json`
FROM
`your_table`
) `jd`;
See dbfiddle.

Extracting Key-worlds out of string and show them in another column

I need to write a query to extract specific names out of String and have them show in another column for example a column has this field
Column:
Row 1: jasdhj31e31jh123hkkj,12l1,3jjds,Amin,02323rdcsnj
Row 2:jasnasc8918212,ahsahkdjjMina67,
Row 3:kasdhakshd,asda,asdasd,121,121,Sina878788kasas
Key Words: Amin,Mina,Sina
How could I have these key words in another column? I dont want to insert another column but if that's the only solution let me know.
Any help appreciated!
Below is for BigQuery Standard SQL
#standardSQL
WITH keywords AS (
SELECT keyword
FROM UNNEST(SPLIT('Amin,Mina,Sina')) keyword
)
SELECT str, STRING_AGG(keyword) keywords_in_str
FROM `project.dataset.table`
CROSS JOIN keywords
WHERE REGEXP_CONTAINS(str, CONCAT(r'(?i)', keyword))
GROUP BY str
You can test, play with above using dummy data from your question as below
#standardSQL
WITH `project.dataset.table` AS (
SELECT 'jasdhMINAj31e31jh123hkkj,12l1,3jjds,Amin,02323rdcsnj' str UNION ALL
SELECT 'jasnasc8918212,ahsahkdjjMina67,' UNION ALL
SELECT 'kasdhakshd,asda,asdasd,121,121,Sina878788kasas'
), keywords AS (
SELECT keyword
FROM UNNEST(SPLIT('Amin,Mina,Sina')) keyword
)
SELECT str, STRING_AGG(keyword) keywords_in_str
FROM `project.dataset.table`
CROSS JOIN keywords
WHERE REGEXP_CONTAINS(str, CONCAT(r'(?i)', keyword))
GROUP BY str
with results as
Row str keywords_in_str
1 jasdhMINAj31e31jh123hkkj,12l1,3jjds,Amin,02323rdcsnj Amin,Mina
2 jasnasc8918212,ahsahkdjjMina67, Mina
3 kasdhakshd,asda,asdasd,121,121,Sina878788kasas Sina
to count the no of keywords
#standardSQL
WITH `project.dataset.table` AS (
SELECT 'jasdhMINAj31e31jh123hkkj,12l1,3jjds,Amin,02323rdcsnj' str UNION ALL
SELECT 'jasnasc8918212,ahsahkdjjMina67,' UNION ALL
SELECT 'kasdhakshd,asda,asdasd,121,121,Sina878788kasas'
)
select str,array(select as struct countif(lower(x) ="amin") amin,countif(lower(x) ="mina") mina,countif(lower(x)="sina") sina from unnest(x)x)keyword from
(select str,regexp_extract_all(str,"(?i)(Amin|Mina|Sina)")x from `project.dataset.table`)

Convert array of strings into array of integers

I have the following SQL:
SELECT * FROM (SELECT t.id, t.summary, null as worker, tt.worked from ticket t
INNER JOIN (SELECT ticket, sum(seconds_worked)/3600.0 as worked FROM ticket_time GROUP BY ticket) tt ON tt.ticket=t.id
UNION ALL
SELECT ticket,null, tt.worker, sum(tt.seconds_worked)/3600.0 from ticket_time tt GROUP BY ticket,worker) as foo
WHERE id in ('9755, 9759') ORDER BY id
The ids string '9755, 9759' in the last line can and will change whenever the sql executed.
I can convert the sting to an array like this:
string_to_array('9755, 9759', ',')
But is there a way to convert this array of strings into array of integers?
Just cast the resulting array to an int[]
where id = ANY ( string_to_array('9755, 9759', ',')::int[] )