Skip to content

Instantly share code, notes, and snippets.

View calebwaldner's full-sized avatar

Caleb Waldner calebwaldner

View GitHub Profile
@calebwaldner
calebwaldner / GetMainStringValue
Last active May 12, 2021 15:48
A BigQuery function that accepts an array of similar strings and returns the string that occurs most within array.
CREATE TEMP FUNCTION GetMainStringValue(arr ARRAY<STRING>) RETURNS STRING AS (
ARRAY(
SELECT list FROM (
SELECT list,
count(*) count,
FROM UNNEST(arr) list
GROUP BY list
ORDER BY count DESC
)
)[SAFE_OFFSET(0)]