pgsql=# select * from resort; rnm | nam | inf | fil -----+--------+-----+----- 1 | グアム | | 2 | 沖縄 | | 3 | タヒチ | | 4 | 江ノ島 | | (4 rows) pgsql=# select * from rvalue; rnm | snm | scn -----+-----+----- 1 | 1 | 11 1 | 2 | 5 1 | 3 | 2 2 | 1 | 12 2 | 2 | 12 2 | 3 | 2 3 | 1 | 4 3 | 2 | 12 3 | 3 | 1 4 | 1 | 15 4 | 2 | 1 (11 rows) pgsql=# select * from question; qnm | flg | inf -----+-----+---------------------- 1 | 0 |何日旅行したいですか? 2 | 1 |どんな旅がいいですか? (2 rows) pgsql=# select * from answer; anm | qnm | inf -------+-----+---------------------------- 10101 | 1 | 日帰り 10102 | 1 | 2泊3日 10103 | 1 | 4泊5日 10104 | 1 | 10日まで 10201 | 2 | とにかく手軽で安い 10203 | 2 | たくさんおみやげを買いたい 10202 | 2 | きれいな海をみたい (7 rows) pgsql=# select * from avalue; anm | snm | scn -------+-----+----- 10101 | 1 | 15 10102 | 1 | 10 10103 | 1 | 5 10104 | 1 | 1 10201 | 1 | 12 10201 | 3 | 7 10202 | 2 | 15 10203 | 3 | 10 (8 rows) pgsql=# select * from krec; knm | anm | val ------+-------+----- 1001 | 10103 | 1 1001 | 10201 | 1 1001 | 10203 | 1 1001 | 10202 | 1 (4 rows) pgsql=# select aa.nam,bb.stl from pgsql-# resort aa, pgsql-# ( pgsql(# select a.rnm as rnm, sum(a.scn * b.atl ) as stl from pgsql(# rvalue a, pgsql(# ( select snm,sum(scn) as atl from avalue where anm in pgsql(# (select anm from krec where knm = 1001 ) pgsql(# group by snm ) b pgsql(# where a.snm = b.snm group by a.rnm pgsql(# ) bb pgsql-# where bb.rnm = aa.rnm; nam | stl --------+----- グアム | 296 沖縄 | 418 タヒチ | 265 江ノ島 | 270 (4 rows)