Column Does Not Exist Error: Order by an Particular Cell of an Array

Поиск
Список
Период
Сортировка
От David Muller
Тема Column Does Not Exist Error: Order by an Particular Cell of an Array
Дата
Msg-id CAMM+bBLf1VOKvGY+X_mz_Ys5+bQV0HRcw34NsL_2Vw5pSywZgw@mail.gmail.com
обсуждение исходный текст
Ответы Re: Column Does Not Exist Error: Order by an Particular Cell of an Array  (Kevin Grittner <kgrittn@gmail.com>)
Список 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 по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Explanation of pg_column_size
Следующее
От: Kevin Grittner
Дата:
Сообщение: Re: Column Does Not Exist Error: Order by an Particular Cell of an Array