I am currently using Bigquery for dynamic generation of query using "GROUP_CONCAT_UNQUOTED" function for concatenation of multiple strings to one.
Example:
SELECT 'SELECT ID, AID, ' +
GROUP_CONCAT_UNQUOTED(
'MAX(IF(KEY = "' + KEY + '", VALUE, NULL)) as [' + KEY + ']'
)
+ ' FROM [project:dataset.tbl] GROUP BY 1,2 ORDER BY 1,2'
FROM (
SELECT KEY
FROM [project:dataset.tbl]
GROUP BY KEY
ORDER BY KEY
)
Above query produces yet another query like below:
SELECT
ID,
AID,
MAX(IF(KEY = "key1", VALUE, NULL)) as [key1],
MAX(IF(KEY = "key2", VALUE, NULL)) as [key2],
MAX(IF(KEY = "key3", VALUE, NULL)) as [key3]
FROM [project:dataset.tbl]
GROUP BY 1,2
ORDER BY 1,2
I would like to do the same using Standard SQL instead of Legacy SQL.
Google docs mentions STRING_AGG() as an alternative but I am not getting the desired output