[pgsql-jp: 35127] Re: テーブルの結合について教えてください

tomson tomson_lpcjp @ yahoo.co.jp
2005年 3月 25日 (金) 16:48:34 JST


tomsonと言います。こんな具合でいいでしょうか?
SQLの世界からいまは離れているので、ただしいかどうかは知りません。

 ・user1テーブル
> |no|name|
> | 1|aaa |
> | 2|bbb |
> 


 ・data1テーブル
> |no|count|score|
> | 1|    1|   50|
> | 1|    2|   60|
> | 1|    3|  120|
> | 2|    1|   70|
> | 2|    2|   80|


select name,
case when tmp.count=1 then tmp.score end as count1,
case when tmp.count=2 then tmp.score end as count2,
case when tmp.count=3 then tmp.score end as count3
from (select * from user1 right outer join data1 on(user1.no=data1.no))
as tmp order by name;

 name | count1 | count2 | count3
------+--------+--------+--------
 aaa  |     50 |        |
 aaa  |        |     60 |
 aaa  |        |        |    120
 bbb  |     70 |        |
 bbb  |        |     80 |
(5 rows)

 



pgsql-jp メーリングリストの案内