Re: postgres question: Views with duplicate field names

From: Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com>
To: Ryan Murphy <ryanfmurphy(at)gmail(dot)com>
Cc: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: Re: postgres question: Views with duplicate field names
Date: 2016-09-05 20:18:48
Message-ID: 40a68d0f-8bed-0867-72ba-cac9f6b7a260@aklaver.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 09/05/2016 01:13 PM, Ryan Murphy wrote:
> Interesting, thanks! Do you know why the first one fails instead of
> doing that renaming process, while your version succeeds?

Because I specifically aliased the first task reference using AS task_1.

>
> On Monday, September 5, 2016, Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com
> <mailto:adrian(dot)klaver(at)aklaver(dot)com>> wrote:
>
> On 09/05/2016 12:55 PM, Ryan Murphy wrote:
>
> Hello, I have a question about views in Postgres.
>
> Given a table like so:
>
> create table todo (
> id serial,
> task text,
> done_time timestamp default null
> );
>
> it is legal (though perhaps not advised, by some) to query it
> like so:
>
> select task, * from todo;
>
> This gives a result with 2 redundant "task" fields (with
> duplicate names):
>
> task | id | task | done_time
> --------------+----+--------------+-----------
> wash the dog | 1 | wash the dog |
>
> However, if I try to make a view of this I hit a problem: views
> can't
> have duplicate field names:
>
> create view task2 as select task, * from todo;
>
> ERROR: column "task" specified more than once
>
> I understand this may seem like a silly thing to want to do, but my
> question is if there is an easy way to automatically de-dup the
> columns
> of the query so I can create a view from it. Or is there any
> fundamental reason why views can't be allowed to have duplicate
> columns,
> just like the result set above?
>
>
> test=> create view task2 as select task AS task_1 , * from todo;
> CREATE VIEW
>
> test=> \d task2
> View "public.task2"
>
> Column | Type | Modifiers
>
> -----------+-----------------------------+-----------
>
> task_1 | text |
>
> id | integer |
> task | text |
> done_time | timestamp without time zone |
>
>
>
> Thanks!
>
> Ryan
>
>
>
> --
> Adrian Klaver
> adrian(dot)klaver(at)aklaver(dot)com
>

--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Patrick B 2016-09-05 21:13:05 Re: Get date timestamp(3) without time zone column - PGSQL 9.5
Previous Message Ryan Murphy 2016-09-05 20:13:21 Re: postgres question: Views with duplicate field names