how to find highest value in a record in Google bigquery

david

I have a table called sample. It contains companies profits. I would like to display the highest profit of each company in Google bigquery.

company   2014      2015      2016      2017
companyA  3453564   5343333   2876345   3465234
companyB  5743231   3276438   1645738   2453657
companyC  3245876   2342879   5876324   7342564

Any help thanks in advance

Mikhail Berlyant

Below is for BigQuery Standard SQL and gives best profit value along with year and also has no dependency on number of year columns and their (columns) names

#standardSQL
WITH `project.dataset.companies` AS (
  SELECT 'companyA' company, 3453564 year_2014, 5343333 year_2015, 2876345 year_2016, 3465234 year_2017 UNION ALL
  SELECT 'companyB', 5743231, 3276438, 1645738, 2453657 UNION ALL
  SELECT 'companyC', 3245876, 2342879, 5876324, 7342564 
)
SELECT 
  company,   
  highest_profit.*
FROM `project.dataset.companies` t, UNNEST(
  ARRAY(
    SELECT AS STRUCT 
      SPLIT(REGEXP_REPLACE(kv, r'[{"}]', ''), ':')[OFFSET(0)] year,
      SAFE_CAST(SPLIT(REGEXP_REPLACE(kv, r'[{"}]', ''), ':')[OFFSET(1)] AS INT64) profit
    FROM UNNEST(SPLIT(TO_JSON_STRING(t), ',"')) kv
    ORDER BY profit DESC
    LIMIT 1
  )
) highest_profit  

with result as

Row company     year        profit   
1   companyA    year_2015   5343333  
2   companyB    year_2014   5743231  
3   companyC    year_2017   7342564   

Update: decided to borrow idea from Mosha's Star Rating post and apply it to time series here and adding kind of sparklines chart

#standardSQL
CREATE TEMP FUNCTION sparklines(arr ARRAY<INT64>) AS ((
  SELECT STRING_AGG(CODE_POINTS_TO_STRING([code]), '') 
  FROM UNNEST(arr) el, 
  UNNEST([(SELECT MAX(el) FROM UNNEST(arr) el)]) mx, 
  UNNEST([(SELECT MIN(el) FROM UNNEST(arr) el)]) mn
  JOIN UNNEST([9602, 9603, 9605, 9606, 9607]) code WITH OFFSET pos
  ON pos = CAST(IF(mx = mn, 1, (el - mn) / (mx - mn)) * 4 AS INT64) 
)); 
WITH `project.dataset.companies` AS (
  SELECT 'companyA' company, 3453564 year_2014, 5343333 year_2015, 2876345 year_2016, 3465234 year_2017 UNION ALL
  SELECT 'companyB', 5743231, 3276438, 1645738, 2453657 UNION ALL
  SELECT 'companyC', 3245876, 2342879, 5876324, 7342564 
)
SELECT 
  company,   
  highest_profit.*,
  sparklines(
    ARRAY(
      SELECT SAFE_CAST(SPLIT(REGEXP_REPLACE(kv, r'[{"}]', ''), ':')[OFFSET(1)] AS INT64) profit
      FROM UNNEST(SPLIT(TO_JSON_STRING(t), ',"')) kv
      WHERE NOT SAFE_CAST(SPLIT(REGEXP_REPLACE(kv, r'[{"}]', ''), ':')[OFFSET(1)] AS INT64) IS NULL
      ORDER BY SPLIT(REGEXP_REPLACE(kv, r'[{"}]', ''), ':')[OFFSET(0)]
    )
  ) history
FROM `project.dataset.companies` t, UNNEST(
  ARRAY(
    SELECT AS STRUCT 
      SPLIT(REGEXP_REPLACE(kv, r'[{"}]', ''), ':')[OFFSET(0)] year,
      SAFE_CAST(SPLIT(REGEXP_REPLACE(kv, r'[{"}]', ''), ':')[OFFSET(1)] AS INT64) profit
    FROM UNNEST(SPLIT(TO_JSON_STRING(t), ',"')) kv
    ORDER BY profit DESC
    LIMIT 1
  )
) highest_profit 

with result as below

Row company     year        profit      history  
1   companyA    year_2015   5343333     ▃▇▂▃     
2   companyB    year_2014   5743231     ▇▅▂▃     
3   companyC    year_2017   7342564     ▃▂▆▇     

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to find the highest sales in each year in BigQuery?

How to find the highest Value with Python

How to find the highest and smallest value

How do I record the highest value in a variable

How to find highest char value in ruby?

How to find the highest value for a column nested in JSON?

How to find the object containing the highest value in an array

how to find the key with the highest value in a dict of dicts?

How to find highest and lowest value on substrings?

Google Sheet: How to find Highest Value along with the date for given set of data(date-value pair) in Google Sheet Formula?

How to get record for highest value based on type and 2 columns

Find the second highest value

Find the highest value in a dataframe

check if a record field contains a value using standard sql (Google BigQuery)

How do I find the highest value and swap it with the end value in an array?

How to find the single highest value? How to show everything if there is a tie?

Find highest value in multidimensional array and keep highest

Javascript: Find variable with highest and second highest value

Find highest value in column insert a new record 1 number higher in said column

How to find the highest N values in each group in Google Sheets

How to iterate through array and find highest value: Java

How to find the highest value of an array index of an HTML class using JQuery

How can i find next highest value in django query

How to find the lowest and highest date based on certain value in another column?

How to find the highest value of a column in a data frame in R?

How to use linq to find the index of the item with the highest value

Objective-C: How to find the highest value in an array without sorting?

How to find highest and lowest value and aggregate into a string in Pandas, Pysimplegui

How to find the key which has the highest value in a c# dictionary?