Unix Technical Forum

Create a table B with data coming from table A

This is a discussion on Create a table B with data coming from table A within the Pgsql General forums, part of the PostgreSQL category; --> My original table is like that: ID A1 A2 A3 cnt 1234 1 0 0 4 1234 1 0 ...


Go Back   Unix Technical Forum > Database Server Software > PostgreSQL > Pgsql General

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 04-09-2008, 05:50 PM
lhaj.merigh@gmail.com
 
Posts: n/a
Default Create a table B with data coming from table A

My original table is like that:

ID A1 A2 A3 cnt
1234 1 0 0 4
1234 1 0 1 8
1234 1 1 1 5
1235 1 0 0 6
1235 1 0 1 7
1235 1 1 1 12

I have to create a new table B:

ID B1 B2 B3 S
1234 4 8 5 17
1235 6 7 12 25

The combination (A1=1,A2=0,A3=0) gives B1
The combination (A1=1,A2=0,A3=0) gives B2
The combination (A1=1,A2=1,A3=1) gives B3

S = B1+B2+B3

I think it's a classical problem, but i can't see to problem key

Thx,
Lhaj

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-09-2008, 05:50 PM
A. Kretschmer
 
Posts: n/a
Default Re: Create a table B with data coming from table A

am Mon, dem 11.06.2007, um 21:23:59 -0000 mailte lhaj.merigh@gmail.com folgendes:
> My original table is like that:
>
> ID A1 A2 A3 cnt
> 1234 1 0 0 4
> 1234 1 0 1 8
> 1234 1 1 1 5
> 1235 1 0 0 6
> 1235 1 0 1 7
> 1235 1 1 1 12
>
> I have to create a new table B:
>
> ID B1 B2 B3 S
> 1234 4 8 5 17
> 1235 6 7 12 25
>
> The combination (A1=1,A2=0,A3=0) gives B1
> The combination (A1=1,A2=0,A3=0) gives B2
> The combination (A1=1,A2=1,A3=1) gives B3
>
> S = B1+B2+B3
>
> I think it's a classical problem, but i can't see to problem key



test=*# select * from tab_a;
id | a1 | a2 | a3 | cnt
------+----+----+----+-----
1234 | 1 | 0 | 0 | 4
1234 | 1 | 0 | 1 | 8
1234 | 1 | 1 | 1 | 5
1235 | 1 | 0 | 0 | 6
1235 | 1 | 0 | 1 | 7
1235 | 1 | 1 | 1 | 12
(6 rows)

test=*# select id, b1,b2,b3, sum(b1+b2+b3) as s from (select id,
sum(case when a1=1 and a2=0 and a3=0 then cnt else 0 end) as b1,
sum(case when a1=1 and a2=0 and a3=1 then cnt else 0 end) as b2,
sum(case when a1=1 and a2=1 and a3=1 then cnt else 0 end) as b3 from
tab_a group by id order by id) foo group by id, b1, b2, b3 order by id;
id | b1 | b2 | b3 | s
------+----+----+----+----
1234 | 4 | 8 | 5 | 17
1235 | 6 | 7 | 12 | 25
(2 rows)



Andreas
--
Andreas Kretschmer
Kontakt: Heynitz: 035242/47150, D1: 0160/7141639 (mehr: -> Header)
GnuPG-ID: 0x3FFF606C, privat 0x7F4584DA http://wwwkeys.de.pgp.net

---------------------------(end of broadcast)---------------------------
TIP 1: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo@postgresql.org so that your
message can get through to the mailing list cleanly

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 12:24 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com