0

I am running a query which is creating a view for me with following details

id      name       brand_id
1        E1          {3,4}
2        E2          {5,7,8}
3        E4          {1}

I want to split the records for brand_id into equal number of rows. Hence the above view should look like:

id      name       brand_id
1        E1          {3}
1        E1          {4}
2        E2          {5}
2        E2          {7}
2        E2          {8}
3        E4          {1}

Over here the brand_id is calculated from a subquery by matching the creation date of record with the date of brand

SQL Query:

CREATE OR REPLACE VIEW %I AS 
    SELECT row_number() over(),
    id,
    name,              
    (select array(select id
                  from brand b
                  where status = true and (i.creation_date = b.creation_date)
                  order by b asc) ) as brand_id
  FROM events i
  group by id order by id

3 Answers 3

1

The simplest is probably:

CREATE VIEW some_name AS
    SELECT i.id, i.name, b.id AS brand_id
    FROM events i
    JOIN brand b USING (creation_date)
    WHERE b.status
    ORDER BY 1;
Sign up to request clarification or add additional context in comments.

3 Comments

Did you miss b.status=true ?
@wingedpanther status is obviously a boolean so just the column name suffices. The WHERE clause needs an expression that evaluates to a boolean, a boolean column will do just fine.
I did not know that, How embarrassing !!
0
select id, name, unnest(brand_id) from events;

Comments

0

Please use below query to extract data.

select id  
      ,name 
      ,'{'||replace(replace(regexp_split_to_table(brand_id, E','),'}',''),'{','')||'}'  
from umang.t_st_ques  
order by 1,2,3;

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.