2

I have this table:

current table

I'm looking for this table:

looking for this table

I have searched for array functions (array_agg) and it didn't work as expected.

How can I approach this task? Would I have to loop into this column?

My dataset:

SELECT 1 id, 'protoc1' protoc UNION ALL
SELECT 2 id, 'protoc2' protoc UNION ALL
SELECT 3 id, 'protoc3' protoc 
1

2 Answers 2

3

Use below

select id, format('%t', array_agg(protoc) over()) protoc
from your_table      

if applied to sample data in your question - output is

enter image description here

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

1 Comment

Mikhail, I really appreciate it, it worked, thanks so much and I will share this post to help that are passing thrugh similar tasks, thanks againg and I will keep on learning!
1

Not exactly sure why you want that output, but I noticed you had the array repeated for all rows, and looked like you wanted the array represented as a specific string -- so here's my attempt at what you're asking:

with sample as (
  SELECT 1 id, 'protoc1' protoc UNION ALL
  SELECT 2 id, 'protoc2' protoc UNION ALL
  SELECT 3 id, 'protoc3' protoc 
), formatted_string as (
  select "[" || ARRAY_TO_STRING(ARRAY_AGG(protoc),', ') || "]" as protoc
  from sample
)
select sample.id, formatted_string.protoc
from formatted_string
cross join sample

Running match of requested output

1 Comment

Mike, thanks so much for sharing your reasoning;, I didn't know about this approach, it also worked! thanks againg for your time and attention, have a good weekend!

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.