Received: from actarg.com (host25.actarg.com [209.180.91.25] (may be forged)) by mail.postgresql.org (8.11.1/8.11.1) with ESMTP id f27NDax99811 for ; Wed, 7 Mar 2001 18:13:36 -0500 (EST) (envelope-from kyle@actarg.com) Received: from chi.actarg.com (IDENT:root@chi.actarg.com [192.168.2.16]) by actarg.com (8.9.3/8.9.3) with ESMTP id QAA29018 for ; Wed, 7 Mar 2001 16:13:33 -0700 Received: from actarg.com (IDENT:kyle@localhost [127.0.0.1]) by chi.actarg.com (8.9.3/8.9.3) with ESMTP id QAA23882 for ; Wed, 7 Mar 2001 16:13:50 -0700 Message-ID: <3AA6C0AD.BC8A015A@actarg.com> Date: Wed, 07 Mar 2001 16:13:50 -0700 From: Kyle Organization: Action Target Inc. X-Mailer: Mozilla 4.7 [en] (X11; I; Linux 2.2.12-20 i686) X-Accept-Language: en MIME-Version: 1.0 To: pgsql-sql@postgresql.org Subject: A query that doesn't work on 7.1 Content-Type: multipart/mixed; boundary="------------5C353FBEEE70421E46362945" X-Archive-Number: 200103/162 X-Sequence-Number: 1815 This is a multi-part message in MIME format. --------------5C353FBEEE70421E46362945 Content-Type: multipart/alternative; boundary="------------37278568DF2323109AEAA9A1" --------------37278568DF2323109AEAA9A1 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Here's a query that doesn't work on 7.1. Is this a bug or am I doing something wrong? The last two selects yield: ERROR: Sub-SELECT uses un-GROUPed attribute m1.ropnum from outer query Basically, everything works until I apply the avg() function and try to aggregate the results. drop table mtr; create table mtr ( ttype varchar(2), --record type ropnum int4, --order number minum int4, --item number pnum varchar(18), tdate date, primary key (ttype,ropnum,minum) ); insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('po',1000,1,2000,'2001-Jan-30'); insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('wm',1000,1,2001,'2001-Jan-10'); insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('wm',1000,2,2002,'2001-Jan-12'); insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('wm',1000,3,2003,'2001-Jan-14'); insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('po',1001,1,2000,'2001-Feb-28'); insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('wm',1001,1,2011,'2001-Feb-01'); insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('wm',1001,2,2012,'2001-Feb-02'); insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('wm',1001,3,2013,'2001-Feb-03'); --The finish date is represented by the tdate of a po type record --The start date is found by the earliest of the wm type records with the same ropnum,minum fields --This lists the start and finish dates select (select min(tdate) from mtr where ttype = 'wm' and ropnum = m1.ropnum) as start, m1.tdate as finish from mtr m1 where m1.ttype = 'po' and m1.pnum = '2000' ; --Now I try to find the average number of days between start and finish for the part select avg(date_part('day',(start::datetime - finish::datetime)::timespan)) from (select (select min(tdate) from mtr where ttype = 'wm' and ropnum = m1.ropnum) as start, m1.tdate::datetime as finish from mtr m1 where m1.ttype = 'po' and m1.pnum = '2000' ) as dates ; --Here I try a different method -- select select avg(date_part('day',((select min(tdate) from mtr where ttype = 'wm' and ropnum = m1.ropnum)::datetime - m1.tdate::datetime)::timespan)) from mtr m1 where m1.ttype = 'po' and m1.pnum = '2000' ; --------------37278568DF2323109AEAA9A1 Content-Type: text/html; charset=us-ascii Content-Transfer-Encoding: 7bit Here's a query that doesn't work on 7.1.  Is this a bug or am I doing something wrong?

The last two selects yield:

ERROR:  Sub-SELECT uses un-GROUPed attribute m1.ropnum from outer query

Basically, everything works until I apply the avg() function and try to aggregate the results.
 

drop table mtr;
create table mtr (
    ttype       varchar(2),     --record type
    ropnum      int4,           --order number
    minum       int4,           --item number
    pnum        varchar(18),
    tdate       date,

    primary key (ttype,ropnum,minum)
);

insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('po',1000,1,2000,'2001-Jan-30');
insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('wm',1000,1,2001,'2001-Jan-10');
insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('wm',1000,2,2002,'2001-Jan-12');
insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('wm',1000,3,2003,'2001-Jan-14');

insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('po',1001,1,2000,'2001-Feb-28');
insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('wm',1001,1,2011,'2001-Feb-01');
insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('wm',1001,2,2012,'2001-Feb-02');
insert into mtr (ttype,ropnum,minum,pnum,tdate) values ('wm',1001,3,2013,'2001-Feb-03');

--The finish date is represented by the tdate of a po type record
--The start date is found by the earliest of the wm type records with the same ropnum,minum fields
--This lists the start and finish dates
select
    (select min(tdate) from mtr where ttype = 'wm' and ropnum = m1.ropnum) as start,
    m1.tdate as finish
    from mtr m1 where
    m1.ttype = 'po' and
    m1.pnum = '2000'
;

--Now I try to find the average number of days between start and finish for the part
select
    avg(date_part('day',(start::datetime - finish::datetime)::timespan)) from
    (select
        (select min(tdate) from mtr where ttype = 'wm' and ropnum = m1.ropnum) as start,
        m1.tdate::datetime as finish
        from mtr m1 where
        m1.ttype = 'po' and
        m1.pnum = '2000'
    ) as dates
;

--Here I try a different method
-- select
select
    avg(date_part('day',((select min(tdate) from mtr where ttype = 'wm' and ropnum = m1.ropnum)::datetime - m1.tdate::datetime)::timespan))
    from mtr m1 where
    m1.ttype = 'po' and
    m1.pnum = '2000'
;
 
  --------------37278568DF2323109AEAA9A1-- --------------5C353FBEEE70421E46362945 Content-Type: text/x-vcard; charset=us-ascii; name="kyle.vcf" Content-Transfer-Encoding: 7bit Content-Description: Card for Kyle Content-Disposition: attachment; filename="kyle.vcf" begin:vcard n:Bateman;Kyle x-mozilla-html:FALSE org:Action Target Inc. adr:;;;;;; version:2.1 email;internet:kyle@actarg.com title:President x-mozilla-cpt:;0 fn:Kyle Bateman end:vcard --------------5C353FBEEE70421E46362945--