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

From: David Muller <dmuller(at)guidebook(dot)com>
To: pgsql-novice(at)postgresql(dot)org
Subject: Column Does Not Exist Error: Order by an Particular Cell of an Array
Date: 2016-08-02 16:48:23
Message-ID: CAMM+bBLf1VOKvGY+X_mz_Ys5+bQV0HRcw34NsL_2Vw5pSywZgw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Kevin Grittner 2016-08-02 21:17:19 Re: Column Does Not Exist Error: Order by an Particular Cell of an Array
Previous Message Tom Lane 2016-08-01 22:13:18 Re: Explanation of pg_column_size