Column Does Not Exist Error: Order by an Particular Cell of an Array - Mailing list pgsql-novice

From David Muller
Subject Column Does Not Exist Error: Order by an Particular Cell of an Array
Date
Msg-id CAMM+bBLf1VOKvGY+X_mz_Ys5+bQV0HRcw34NsL_2Vw5pSywZgw@mail.gmail.com
Whole thread Raw
Responses Re: Column Does Not Exist Error: Order by an Particular Cell of an Array  (Kevin Grittner <kgrittn@gmail.com>)
List pgsql-novice

Hello,

I have a question about ordering records based on a particular cell of a one dimensional Array.


For example, say I create a simple table (that just stores integers)...

```

gears=> CREATE TABLE foo ( value integer PRIMARY KEY);

CREATE TABLE

gears=> insert into foo Values(1);

INSERT 0 1

gears=> insert into foo Values(2);

INSERT 0 1

gears=> select * from foo;

 value 

-------

     1

     2

(2 rows)

```



Now, say I want to run a SELECT statement that also annotates each row with a an array of values, and orders the results by that new SELECTed array -- in this example we simply annotate every row with an integer array like {4,5}:

```

gears=> select *, Array[4,5] as array_from_select from foo order by array_from_select;

 value | array_from_select 

-------+-------------------

     1 | {4,5}

     2 | {4,5}

(2 rows)

```


This appears to work OK (although, of course, since every row has {4,5} annotated to it, the ORDER BY clause is pretty meaningless). 


I'm confused, however, why I receive, "ERROR:  column "array_from_select" does not exist" when I try to order the results by a specific index of my newly selected array. For example, when I try to order the results by the first cell of the Array, I get...

```

gears=> select *, Array[4,5] as array_from_select from foo order by array_from_select[1];

ERROR:  column "array_from_select" does not exist

LINE 1: ...Array[4,5] as array_from_select from foo order by array_from...  

```


Thanks for you insights and time!


- David

pgsql-novice by date:

Previous
From: Tom Lane
Date:
Subject: Re: Explanation of pg_column_size
Next
From: Kevin Grittner
Date:
Subject: Re: Column Does Not Exist Error: Order by an Particular Cell of an Array