Received: from makus.postgresql.org (makus.postgresql.org [98.129.198.125]) by mail.postgresql.org (Postfix) with ESMTP id 39DC61B5234A for ; Wed, 23 May 2012 07:33:45 -0300 (ADT) Received: from hub.ringways.co.uk ([77.86.27.30] helo=mail.ringways.co.uk) by makus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1SX8sy-0000cZ-8J for pgsql-sql@postgresql.org; Wed, 23 May 2012 10:33:45 +0000 Received: from localhost ([127.0.0.1] helo=mail.ringways.co.uk) by mail.ringways.co.uk with esmtp (Exim 4.69) (envelope-from ) id 1SX8sf-0005mS-En for pgsql-sql@postgresql.org; Wed, 23 May 2012 11:33:26 +0100 Received: from eddie.ringways.co.uk ([10.1.1.115] helo=eddie.ringways.co.uk) by mail.ringways.co.uk with ESMTP id qBXIm4SU49327; Wed, 23 May 2012 11:33:18 +0100 From: Gary Stainburn Organization: Ringways Garages Ltd To: pgsql-sql@postgresql.org Subject: Re: left outer join only select newest record Date: Wed, 23 May 2012 11:33:21 +0100 User-Agent: KMail/1.9.10 References: <201205231027.39198.gary.stainburn@ringways.co.uk> In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201205231133.21123.gary.stainburn@ringways.co.uk> X-SpamTest-Envelope-From: gary.stainburn@ringways.co.uk X-SpamTest-Info: Profiles 20401 [Mar 31 2011] X-SpamTest-Method: none X-SpamTest-Rate: 0 X-SpamTest-Status: Not detected X-SpamTest-Status-Extended: not_detected X-SpamTest-Version: SMTP-Filter Version 3.0.0 [0285], KAS30/SDK/Release X-Anti-Virus: Kaspersky Mail Gateway, version: 5.6.28/RELEASE, bases: 20110331T110535 #5151353, check: 20120523 clean X-Spam-Score: -51.6 (---------------------------------------------------) X-Spam-Report: Spam detection software, running on the system "ollie.ringways.co.uk", has identified this incoming email as possible spam. The original message has been attached to this so you can view it (if it isn't spam) or label similar future email. If you have any questions, see Gary Stainburn for details. Content preview: On Wednesday 23 May 2012 10:46:02 Pavel Stehule wrote: > select distinct on (s.s_registration) * > ... order by u.ud_id desc I tried doing this but it complained about the order by. goole=# select distinct on (s.s_stock_no) s_stock_no, s_regno, s_vin, s_created, ud_id, ud_handover_date from stock s left outer join used_diary u on s.s_regno = u.ud_pex_registration where s_stock_no = 'UL15470' order by s_stock_no, ud_id desc; ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions goole=# select distinct on (s.s_stock_no) s_stock_no, s_regno, s_vin, s_created, ud_id, ud_handover_date from stock s left outer join used_diary u on s.s_regno = u.ud_pex_registration where s_stock_no = 'UL15470' order by ud_id desc; ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions goole=# [...] Content analysis details: (-51.6 points, 15.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -50 ALL_TRUSTED Passed through trusted hosts only via SMTP -2.6 BAYES_00 BODY: Bayesian spam probability is 0 to 1% [score: 0.0000] 1.0 RING_SAFE RING_SAFE X-Pg-Spam-Score: -1.9 (-) X-Archive-Number: 201205/72 X-Sequence-Number: 36620 On Wednesday 23 May 2012 10:46:02 Pavel Stehule wrote: > select distinct on (s.s_registration) * > ... order by u.ud_id desc I tried doing this but it complained about the order by. goole=# select distinct on (s.s_stock_no) s_stock_no, s_regno, s_vin, s_created, ud_id, ud_handover_date from stock s left outer join used_diary u on s.s_regno = u.ud_pex_registration where s_stock_no = 'UL15470' order by s_stock_no, ud_id desc; ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions goole=# select distinct on (s.s_stock_no) s_stock_no, s_regno, s_vin, s_created, ud_id, ud_handover_date from stock s left outer join used_diary u on s.s_regno = u.ud_pex_registration where s_stock_no = 'UL15470' order by ud_id desc; ERROR: SELECT DISTINCT ON expressions must match initial ORDER BY expressions goole=# > > or > > select * > from stock_details s > left join (select * from used_diary where (ud_id, > ud_registration) = (select max(ud_id), ud_registration from used_diary > group by ud_registration)) x > on s.s_registration = x.ud_registration; > This was more like what I was thinking, but I still get an error, which I don't understand. I have extracted the inner sub-select and it does only return one record per registration. (The extra criteria is just to ignore old or cancelled tax requests and doesn't affect the query) goole=# select distinct on (s.s_stock_no) s_stock_no, s_regno, s_vin, s_created, ud_id, ud_handover_date from stock s left outer join (select ud_id, ud_pex_registration, ud_handover_date from used_diary where (ud_id, ud_pex_registration) = (select max(ud_id), ud_pex_registration from used_diary where (ud_tab is null or ud_tab <> 999) and ud_created > CURRENT_DATE-'4 months'::interval group by ud_pex_registration)) udIn on s.s_regno = udIn.ud_pex_registration; ERROR: more than one row returned by a subquery used as an expression