I am trying to find out the way to filter out records based on query but I have no clue how to do it in same query:
Query:
select unnest(array_cat(a.object_references, a.dependent_object_references))
from mgmt.log_metrics a
Input:
Select will provide records in below format:
(a,b,**r**,)
(c,d,**r**,)
(e,l,v,)
(g,h,i,f,)
In input
- r stands for table,
- v stands for view
- f stands for function
and in output I need to filter based on r so that I will get all the table records but not sure how to achieve it using above query.Can anyone please help.
Output:
(a,b,**r**,)
(c,d,**r**,)
I have tried to apply where clause and also tried to put substring but after doing research found that substring will not work directly with unnest.Also tried Right function but I am new so not sure how to achieve this hence need help with this.
2
Answers
Calling a table function or a set-returning function like
unnest()
in theselect
list is pretty common, but that’s more of a syntactic shortcut rather than a normal place for them. Your code effectively does this:When you structure it like that, it becomes a bit clearer that you can simply add a
where
to filter out some of the elements resulting fromunnest
ing the concatenated arrays: demo at db<>fiddleIf you prefer to modify the array before unnesting it, you can use
array_remove()
:Asssuming that your filter condition should be on the last element of an array ("… Also tried Right function …") maybe this could help you find your way out of this:
See dbfiddle here.