0

I am trying to sort out how to change this array so that it shows the returned values across multiple columns instead of together.

SELECT
handle,
fdc.identifiers
FROM table.fdc

Code returns the following

Handle Identifier
1234 3123
1233
1232
3456 1231
3411
2321
1235

The code returns all of the identifiers in one column. Can they be split out to multiple columns?

I would like the table to look like this below:

Handle Identifier
1234 3123 1233 1232
3456 1231 3411
2321
1235

The code returns all of the identifiers in one column. Can they be split out to multiple columns?

4
  • Can you post an example of what the output table should look like? Commented Dec 3, 2021 at 3:15
  • Edited the post with a new table to demonstrate what I need. Each Handle can have a unique amount of identifiers or be null. The extra columns can be labeled if needed. Commented Dec 3, 2021 at 3:33
  • If the number of identifiers is indeterminate it might make more sense to return them in an ARRAY column. See this answer by Mikhail Berlyant: stackoverflow.com/a/41110942/15921941 Commented Dec 3, 2021 at 3:40
  • The results are already in an array, so any type of array_agg function doesn't work. Any other ideas? Commented Dec 3, 2021 at 4:35

1 Answer 1

1

Consider below approach

select * from (
  select handle, val, offset
  from your_table t
  left join t.identifiers val with offset
)
pivot (min(val) as identifier for offset in (0, 1, 2))    

if to apply to dummy data as in your question

with your_table as (
  select 1234 handle, [3123, 1233, 1232] identifiers union all
  select 3456, [1231, 3411] union all
  select 2321, [] union all 
  select 1235, []
)    

output is

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

thanks for the help! I literally have 0 understanding of what's going on in your code. any chance you can explain it? specifically with offset and pivot, never used them before. I also need to have a where clause that specifies a date range. I tried to add that to the code and it couldn't recognize any date range no matter how I labeled anything.

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.