Thread: unnest in SELECT

unnest in SELECT

From
Karl Koster
Date:
I have a couple of questions regarding unnest.

1) If I issue a select statement "select unnest(vector1) as v from 
some_table", I cannot seem to use the column alias v in a WHERE or 
HAVING clause. I can use it in an ORDER BY or GROUP by clause. Is this 
the way it is supposed to work?

2) If I issue a select statement "select unnest(vector1) as v1, 
unnest(vector2) as v2 from some_table" and vector1 has a length of 3 and 
vector2 has a length of 4, the result set will have 12 rows with the 
data of vector1 repeating 4 times and vector2 repeating 3 times. 
Shouldn't the content of the shorter array(s) simply be return null in 
it's respective column and the result set be the size of the longest array?

Thanks,
Karl


Re: unnest in SELECT

From
Andreas Kretschmer
Date:
Karl Koster <klkoster@optonline.net> wrote:

> I have a couple of questions regarding unnest.
>
> 1) If I issue a select statement "select unnest(vector1) as v from  
> some_table", I cannot seem to use the column alias v in a WHERE or  
> HAVING clause. I can use it in an ORDER BY or GROUP by clause. Is this  
> the way it is supposed to work?

Yes. It's the same as:

test=# select 1 as a where a = 2;
ERROR:  column "a" does not exist
LINE 1: select 1 as a where a = 2;

in other words: the column a is unknown for the where-condition at this
time.

>
> 2) If I issue a select statement "select unnest(vector1) as v1,  
> unnest(vector2) as v2 from some_table" and vector1 has a length of 3 and  
> vector2 has a length of 4, the result set will have 12 rows with the  
> data of vector1 repeating 4 times and vector2 repeating 3 times.

Yeah, it's a cross-join.



Andreas
-- 
Really, I'm not out to destroy Microsoft. That will just be a completely
unintentional side effect.                              (Linus Torvalds)
"If I was god, I would recompile penguin with --enable-fly."   (unknown)
Kaufbach, Saxony, Germany, Europe.              N 51.05082°, E 13.56889°