0

Is it possible to take an aliased column from the outer query and use it to filter a subquery. In essential, I'd like to do something like this

select distinct 
    array_agg(foo.id) as foo_list,
    (select sum(data) from foo where id in foo_list)
from foo
where x

but syntax error at or near "foo_list"

devReddit
  • 2,696
  • 1
  • 5
  • 20
Mcscrag
  • 55
  • 6

1 Answers1

0

Try this:

with temp as
(
  select distinct array_agg(foo.id) as foo_list from foo
)
select sum(data) from foo 
where id in (select  foo_list from temp)
Mitko Keckaroski
  • 954
  • 1
  • 8
  • 12