0

Imagine you have a simple table

  Key c1 c2 c3
A id1  x  y  z
B id2  q  r  s

what I would like is a query that gives me the result as 2 arrays so something like

Select
id1,
ARRAY_MAGIC_CREATOR(c1, c2, c3)
from Table

With the result being

id1, <x,y,z>
id2, <q,r,s>

Almost everything I have searched for end up converting rows to arrays or other similar sounding but very different requests.

Does something like this even exist in SQL? Please note that the data type is NOT a string so we can't use string concat. They are all going to be treated as floats.

3 Answers 3

3

It is called ARRAY:

Select id1, ARRAY[c1, c2, c3] as c_array
from Table
Sign up to request clarification or add additional context in comments.

Comments

1

This will also work :o)

select key, [c1, c2, c3] c
from `project.dataset.table`  

Comments

1

Consider below generic option which does not require you to type all columns names or even to know them in advance - more BigQuery'ish way of doing business :o)

select key, 
  regexp_extract_all(
    to_json_string((select as struct * except(key) from unnest([t]))),
    r'"[^":,]+":([^":,]+)(?:,|})'
  ) c
from `project.dataset.table` t    

If applied to sample data in your question - output is

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.