1

I have a select query return and it shows the result like below:

select * from table gives the result like below

enter image description here

I have parameter called Apple If I pass the parameter somewhere in query I should get the result like below

enter image description here

How to get this in postgresql. If anyone knows please share the answer below.

2
  • Sample data is better presented as formatted text. See here for some tips on how to create nice looking tables. Commented Aug 13, 2020 at 7:19
  • PostgreSQL 10.10 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9), 64-bit Commented Aug 13, 2020 at 7:24

2 Answers 2

2

I would do this with a helper function for clarity. And it might be reusable.

create or replace function filter_jsonb_array(arr jsonb, fruit text) 
returns jsonb language sql immutable as 
$$
select coalesce
(
 (select jsonb_agg(j) from jsonb_array_elements(arr) j where j ->> 'fruit' = fruit),
 '[]'::jsonb
);
$$;

and then

select "Column_A", "Column_B", filter_jsonb_array("Column_JSONARRAY", 'Apple') from table_;

If you do not want a function then the function body can be placed directly into the select query.

select 
  "Column_A", 
  "Column_B",
  coalesce
  (
   (select jsonb_agg(j) from jsonb_array_elements("Column_JSONARRAY") j where j ->> 'fruit' = 'Apple'),
   '[]'::jsonb
  ) "Column_JSONARRAY"
 from table_;
Sign up to request clarification or add additional context in comments.

11 Comments

i used the 2nd option which was not using functions and it was perfectly fine. But my next concern is, what i should use if the date type of fruits was a character varying [] arary? I tried this where 'mango'=ANY(j->>'fruit') But couldnt get result properly
Try = any((select array_agg(t) from jsonb_array_elements_text(j->'fruit') t)::text[]). It will only work if fruit is an array.
Used the above. I got the answer which i expected. Any my final question is I want to use LOWER of function in my parameter. How to do it?
I would rather lower both the parameter and the array values. So here it is. where lower('Mango') = any((select array_agg(lower(t)) from jsonb_array_elements_text(j->'fruit') t)::text[])
You are a genius. What I looked for, I completely got it. Thank you to the core
|
0

Considering your datatype of column Column_JSONARRAY is JSONB, try This:

with cte as (
   SELECT column_a, column_b,  (column_jsonarray ->> ( index_-1 )::int)::jsonb  AS "column_jsonarray"
     FROM table_
    CROSS JOIN jsonb_array_elements(column_jsonarray)
     WITH ORDINALITY arr(array_,index_)
    WHERE array_->>'fruit' in ('Apple') 
)

select t1.column_a, t1.column_b, jsonb_agg(t2.column_jsonarray) 
from table_ t1 
left join cte t2 on t1.column_a =t2.column_a and t1.column_b =t2.column_b

group by t1.column_a, t1.column_b

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.