0

I have a JSON map/dictionary where I want to find the largest value in it, but I’m having issues writing a UDF (using Legacy SQL syntax). I couldn’t find many resources or examples of this online, but I did see many for Standard SQL. Could anyone provide help or good online examples of dealing with maps, dictionaries, and user defined functions using Legacy SQL? Thanks!

Example input: {“cat_age”: 14, “dog_age”: 4} (this is a column in my table, let’s call the column name “AgeColumn”)

Output: 14

1 Answers1

0

Below is for BigQuery Standard SQL

#standardSQL
SELECT col, 
  (
    SELECT AS STRUCT CAST(SPLIT(item, ':')[SAFE_OFFSET(1)] AS INT64) age, item 
    FROM UNNEST(SPLIT(REGEXP_REPLACE(col, r'{|}', ''))) item
    ORDER BY CAST(SPLIT(item, ':')[SAFE_OFFSET(1)] AS INT64) DESC
    LIMIT 1
  ).*
FROM `project.dataset.table`   

You ca test, play with above using dummy data as in below example

#standardSQL
WITH `project.dataset.table` AS (
  SELECT '{“cat_age”: 14, “dog_age”: 4}' col UNION ALL
  SELECT '{“cat_age”: 11, “dog_age”: 16}' 
)
SELECT col, 
  (
    SELECT AS STRUCT CAST(SPLIT(item, ':')[SAFE_OFFSET(1)] AS INT64) age, item 
    FROM UNNEST(SPLIT(REGEXP_REPLACE(col, r'{|}', ''))) item
    ORDER BY CAST(SPLIT(item, ':')[SAFE_OFFSET(1)] AS INT64) DESC
    LIMIT 1
  ).*
FROM `project.dataset.table`   

with result

Row     col                                 age     item     
1       {“cat_age”: 14, “dog_age”: 4}       14      “cat_age”: 14    
2       {“cat_age”: 11, “dog_age”: 16}      16      “dog_age”: 16    
Mikhail Berlyant
  • 165,386
  • 8
  • 154
  • 230