简体   繁体   中英

postgres select where from rows generated by regexp_split_to_table with union throws error

I have a table 'my_table' with two columns(c1,c2) holding data in the exact same format '1,2,3,4;5,6,7,8' I use regexp_split_to_table to split them into rows, and I'm trying to select non-empty rows.

SELECT * from 
 (SELECT '1' as param1 ,'2' as param2, param3 FROM
  ((select regexp_split_to_table(my_table.c1, ';') as param3
   FROM  my_table)
 UNION
  (select regexp_split_to_table(my_table.c2, ';') as param3
   FROM  my_table)) AS a) as b

 where param3 is not null

running this query gives the following error:

set-valued function called in context that cannot accept a set

if I remove the union and select only from one of the columns, or filter by one of the other fields (param1/param2), the query works. I thought it might be an issue with the data so I tried selecting from the same column twice:

SELECT * from 
 (SELECT '1' as param1 ,'2' as param2, param3 FROM
  ((select regexp_split_to_table(my_table.c1, ';') as param3
   FROM  my_table)
 UNION
  (select regexp_split_to_table(my_table.c1, ';') as param3
   FROM  my_table)) AS a) as b

 where param3 is not null

but I still get the same error. does anybody know how to solve this? thanks!

I'd suggest using CTE instead of subqueries and format your queries. Here's the query I think you need:

with cte1 as (
  select regexp_split_to_table(t.c1, ';') as param3
  from my_table as t
  union all
  select regexp_split_to_table(t.c2, ';') as param3
  from my_table as t
)
select '1' as param1 ,'2' as param2, c.param3
from cte1 as c
where c.param3 is not null

sql fiddle demo

note: I don't know if you need duplicates or not in your query, so if you don't need it - use union instead of union all

update

with cte1 as (
  select regexp_split_to_table(t.c1, ';') as param3
  from my_table as t
  union all
  select regexp_split_to_table(t.c2, ';') as param3
  from my_table as t
), cte2 as (
  select regexp_split_to_table(c.param3, ',') as param3
  from cte1 as c
  where c.param3 is not null and c.param3 <> ''
)
select *
from cte2 as c
where c.param3 is not null and c.param3 <> ''

sql fiddle demo

update2 it could be even done with one regexp

with cte1 as (
    select c1 as param3 from my_table
    union all
    select c2 as param3 from my_table
), cte2 as (
  select regexp_split_to_table(param3, '(;|,)') as param3
  from cte1
)
select '1' as param1 ,'2' as param2, c.param3
from cte2 as c
where nullif(c.param3, '') <> ''

sql fiddle demo

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM