From: | Tomasz Myrta <jasiek(at)klaster(dot)net> |
---|---|
To: | Gary Stainburn <gary(dot)stainburn(at)ringways(dot)co(dot)uk> |
Cc: | PostgreSQL-sql <pgsql-sql(at)postgresql(dot)org> |
Subject: | Re: sub-sel/group problem |
Date: | 2003-08-11 13:49:09 |
Message-ID: | 3F379ED5.3030408@klaster.net |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-sql |
> 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
From | Date | Subject | |
---|---|---|---|
Next Message | Kurt Overberg | 2003-08-11 16:51:31 | Query suddenly taking longer.... |
Previous Message | Stephan Szabo | 2003-08-11 13:48:31 | Re: [pg-sql] - Problem with duplicate table (PostgreSQL 7.2.3 |