如何在Google bigquery的记录中找到最高价值

时间:2018-02-27 10:29:22

标签: google-bigquery

我有一张名为sample的表。它包含公司的利润。我想在Google bigquery中展示每家公司的最高利润。

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

提前感谢任何帮助

2 个答案:

答案 0 :(得分:2)

一种方法是使用GREATEST(请参阅here):

WITH
  input AS (
  SELECT
    'companyA' AS company,
    3453564 AS y2014,
    5343333 AS y2015,
    2876345 AS y2016,
    3465234 AS y2017
  UNION ALL
  SELECT
    'companyB' AS company,
    5743231 AS y2014,
    3276438 AS y2015,
    1645738 AS y2016,
    2453657 AS y2017
  UNION ALL
  SELECT
    'companyC' AS company,
    3245876 AS y2014,
    2342879 AS y2015,
    5876324 AS y2016,
    7342564 AS y2017)
SELECT
  company,
  GREATEST(y2014,y2015,y2016,y2017) AS max_profit
FROM
  input

输出:

Row company     max_profit   
1   companyA    5343333  
2   companyB    5743231  
3   companyC    7342564

答案 1 :(得分:1)

下面是BigQuery Standard SQL,它提供了最佳的利润价值以及年份,也不依赖于年份列数及其(列)名称

   
#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  

结果为

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

更新:决定借用Mosha的Star Rating帖子的想法并将其应用于此处的时间序列并添加一些迷你图表

#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 

结果如下

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