Re: How to append tables in a view

From: Shane Ambler <pgsql(at)007Marketing(dot)com>
To: Shoaib Mir <shoaibmir(at)gmail(dot)com>
Cc: Ashish Karalkar <ashish(dot)karalkar(at)info-spectrum(dot)com>, pgsql-general(at)postgresql(dot)org
Subject: Re: How to append tables in a view
Date: 2007-02-13 09:51:26
Message-ID: 45D18A1E.2050207@007Marketing.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Shoaib Mir wrote:
> I guess UNION ALL should work good here instead of a UNION for the exact
> same kind of output he needs:

That would be UNION ordering the results to remove duplicate rows which
UNION ALL doesn't do. Technically the results from any query can come
back in any order unless an ORDER BY is included.

> SELECT id,desc FROM table1
> UNION ALL
> SELECT id,desc FROM table2;
>
> ---+---
> 1 | A
> 2 | B
> 3 | C
> 1 | D
> 2 | E
> 3 | F
>
>
> As UNION gave me a little different output, like this:
>
> ---+--
> 1 | A
> 1 | D
> 2 | B
> 2 | E
> 3 | C
> 3 | F
>
> --
> Shoaib Mir
> EnterpriseDB (www.enterprisedb.com)
>
> On 2/13/07, Shane Ambler <pgsql(at)007marketing(dot)com> wrote:
>>
>> Ashish Karalkar wrote:
>> > Hello List,
>> >
>> > I want to append column of two different tables in a single column
>> of a
>> view .
>> >
>> > data type of tow column of two diffrent tables will be same.
>> >
>> > WHAT I WANT TO DO IS:
>> >
>> > Table 1
>> > ID DESC
>> > 1 A
>> > 2 B
>> > 3 C
>> >
>> >
>> >
>> >
>> >
>> > Table 2
>> > ID DESC
>> > 1 D
>> > 2 E
>> > 3 F
>> >
>> >
>> >
>> > View(Table1|| Table 2)
>> > ID_view Desc
>> >
>> > 1 A
>> > 2 B
>> > 3 C
>> > 4 D
>> > 5 E
>> > 6 F
>> >
>> >
>> >
>> > Is there any way???
>>
>> A union -
>>
>> SELECT id,desc FROM table1
>> UNION
>> SELECT id,desc FROM table2;
>>
>> This will give you
>>
>> ID_view Desc
>>
>> 1 A
>> 2 B
>> 3 C
>> 1 D
>> 2 E
>> 3 F
>>
>>
>> If you actually want the id_view column to show 1 through 6 then you
>> will want to generate a sequence that is shown for that column instead
>> of the original id column. Or generate the id_view in the client, such
>> as use the row position in the returned set.
>>
>>
>>
>>
>> --
>>
>> Shane Ambler
>> pgSQL(at)007Marketing(dot)com
>>
>> Get Sheeky @ http://Sheeky.Biz
>>
>

--

Shane Ambler
pgSQL(at)007Marketing(dot)com

Get Sheeky @ http://Sheeky.Biz

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message hubert depesz lubaczewski 2007-02-13 09:53:52 Re: Adjacency List or Nested Sets to model file system hierarchy?
Previous Message Pankaj 2007-02-13 09:49:42 help and Links using postgreSQL with ASP