[pgsql-jp: 31310] Re: PL_pgSQL関数の正しい利用方法について

ISHIDA Akio iakio @ pjam.jpweb.net
2003年 10月 26日 (日) 10:54:07 JST


こんにちは。石田@苫小牧市です。

Yamada Ken Takeshi wrote:
>   山田@町田市です。
>   便乗質問で恐縮ですが、下のようにファンクション同士の join を
> 取ろうとすると、group by のところで
> ERROR:  column "thisyr.id_company" must appear in the 
>         GROUP BY clause or be used in an aggregate function
> というエラーが出てしまいますが、何か間違っているのでしょうか?
> (エラーの内容の指示に従うと、目的とするものは得られません。)

エラーの内容の指示に従うと、目的とするものは得られません、というのは、

select thisyr.id_company, thisyr.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 thisyr.id_company,thisyr.pref_code
       , prevyr.id_company,prevyr.pref_code
order by thisyr.pref_code, thisyr.id_company

では目的のものは得られないということですか?

> 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

これだと plpgsql を使っていなくてもエラーになると思います。

ishida=> CREATE TABLE t(i int);
CREATE TABLE
ishida=> SELECT * FROM t AS t1 FULL JOIN t AS t2 USING (i) GROUP BY i;
ERROR:  column "t1.i" must appear in the GROUP BY clause or be used in
an aggregate function

--
ISHIDA Akio<iakio @ pjam.jpweb.net>





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