X-Original-To: pgsql-sql-postgresql.org@localhost.postgresql.org Received: from localhost (unknown [64.117.224.130]) by svr1.postgresql.org (Postfix) with ESMTP id 4DDBCD1C514 for ; Mon, 11 Aug 2003 13:48:45 +0000 (GMT) Received: from svr1.postgresql.org ([64.117.224.193]) by localhost (neptune.hub.org [64.117.224.130]) (amavisd-new, port 10024) with ESMTP id 00683-01 for ; Mon, 11 Aug 2003 10:47:19 -0300 (ADT) Received: from serwer.skawsoft.com.pl (serwer.skawsoft.com.pl [213.25.37.66]) by svr1.postgresql.org (Postfix) with ESMTP id 40857D1C4D6 for ; Mon, 11 Aug 2003 10:48:34 -0300 (ADT) Received: from klaster.net (core-1.citynet.pl [80.48.135.69]) by serwer.skawsoft.com.pl (Postfix) with ESMTP id 885DB2B3CE; Mon, 11 Aug 2003 15:48:21 +0200 (CEST) Message-ID: <3F379ED5.3030408@klaster.net> Date: Mon, 11 Aug 2003 15:49:09 +0200 From: Tomasz Myrta User-Agent: Mozilla/5.0 (Windows; U; Win 9x 4.90; pl-PL; rv:1.5a) Gecko/20030718 X-Accept-Language: pl, en-us, en MIME-Version: 1.0 To: Gary Stainburn Cc: PostgreSQL-sql Subject: Re: sub-sel/group problem References: <200308111124.07832.gary.stainburn@ringways.co.uk> In-Reply-To: <200308111124.07832.gary.stainburn@ringways.co.uk> Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit X-Virus-Scanned: by amavisd-new at postgresql.org X-Archive-Number: 200308/102 X-Sequence-Number: 14718 > Hi folks, > > I don;t know if it's cos it's Monday or what, but I can't see what's wrong > here. > > I've got two tables, turns which holds a turn number, a task, and where > appropriate a link to a loco. The select below works but only shows those > tasks where a loco is involved.: > > select r.rtid, concat(r.rcid::text || ' on ' || l.lnumber::text) as task > from rides r, loco_dets l where r.rlid = l.lid group by rtid; > > rtid | task > ------+--------------------------------------------- > 5 | G on 60007 > 6 | A on 75014, C on 75014, A on 75029, C on 75029 > 7 | C on 4277, A on 44767, C on 44767 > 8 | A on 30926, C on 30926, G on 60532 > 9 | A on 30926, C on 30926, A on 75014, C on 75014 > 10 | F on 2392, F on 75029, L on 75029 > 11 | A on 44767, C on 44767, A on 75029 > > However, when I tried to change this to using an outer join I'm getting stuck. > Can anyone see my stumbling point, which I think is to do with the condition > part of the case statement. Do I need to do that in a sub-select first or is > there an alternative? > > =# select r.rtid, > -# case when r.rlid > 0 then > -# concat(r.rcid::text || ' on ' || l.lnumber::text) > -# else > -# r.rcid::text > -# end as task > -# from rides r > -# left outer join loco_dets l on r.rlid = l.lid > -# group by rtid > -# order by rtid > -# ; > ERROR: Attribute r.rlid must be GROUPed or used in an aggregate function > =# Watch out. When using outer joins you get NULL values. some_text || NULL = NULL (always) Try this: select r.rtid, concat(r.rcid::text || coalesce(' on ' || l.lnumber::text,'') as task from rides r left outer join loco_dets l on r.rlid = l.lid group by r.rtid order by r.rtid Regards, Tomasz Myrta