[pgsql-jp: 31308] Re: PL_pgSQL関数の正しい利用方法について
Yamada Ken Takeshi
ken @ tydfam.jp
2003年 10月 26日 (日) 10:26:17 JST
山田@町田市です。
便乗質問で恐縮ですが、下のようにファンクション同士の join を
取ろうとすると、group by のところで
ERROR: column "thisyr.id_company" must appear in the
GROUP BY clause or be used in an aggregate function
というエラーが出てしまいますが、何か間違っているのでしょうか?
(エラーの内容の指示に従うと、目的とするものは得られません。)
元テーブルは
create table foo (
id_prod integer not null,
id_company char(3) not null,
pref_code integer not null,
year_month integer not null,
volume float8 not null
);
で月毎県毎製品毎販売会社毎のデータとなっており、前年対比で
県毎販売会社毎の数字を一発で見たいと考えているのですが、、、。
year2date(yyyyxx) は yyyy年のxx 月までのデータを抽出して
SETOF RECORD として返して来ているだけです。
(同様のことは別の方法<sub query>で出来ますが、可読性を上げる
ために sub query 部分を function にして使えないかなと思って
ちょっといじって見た結果です)
select id_company, pref_code,
sum(thisyr.volume) as "2003",
sum(prevyr.volume) as "2002"
from year2date(200309) as
thisyr(id_prod int4, id_company char(3), pref_code int,
volume float8)
full join
year2date(200209) as
prevyr(id_prod int4, id_company char(3), pref_code int,
volume float8)
using(id_prod, id_company, pref_code)
where id_prod in (1, 191,192)
group by id_company,pref_code
order by pref_code, id_company
pgsql-jp メーリングリストの案内