Received: from makus.postgresql.org ([98.129.198.125]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TRfny-0005nu-MT for pgsql-sql@postgresql.org; Fri, 26 Oct 2012 09:02:15 +0000 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 1TRfnt-0002Yf-5S for pgsql-sql@postgresql.org; Fri, 26 Oct 2012 09:02:13 +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 1TRfnm-0005hm-Or for pgsql-sql@postgresql.org; Fri, 26 Oct 2012 10:02:04 +0100 Received: from eddie.ringways.co.uk ([10.1.1.115] helo=eddie.ringways.co.uk) by mail.ringways.co.uk with ESMTP id qA209WHZ24314; Fri, 26 Oct 2012 10:02:00 +0100 From: Gary Stainburn Organization: Ringways Garages Ltd To: pgsql-sql@postgresql.org Subject: pull in most recent record in a view Date: Fri, 26 Oct 2012 10:02:01 +0100 User-Agent: KMail/1.9.10 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <201210261002.02001.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: 20121026 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: I know I've asked a similar question before but I can't find it. I'm doing a new project for a charity which involves managing a skills / requesit matrix. For each skill type / staff id I need to keep a record of when the skill was aquired/renewed and when it expires. [...] 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] 0.0 AWL AWL: From: address is in the auto white-list 1.0 RING_SAFE RING_SAFE X-Pg-Spam-Score: 0.1 (/) X-Archive-Number: 201210/54 X-Sequence-Number: 36925 I know I've asked a similar question before but I can't find it. I'm doing a new project for a charity which involves managing a skills / requesit matrix. For each skill type / staff id I need to keep a record of when the skill was aquired/renewed and when it expires. Put simply skills=# select * from staff; st_id | st_name -------+--------- 1 | Gary (1 row) skills=# select * from skills; sk_id | sk_desc | sk_renewal -------+---------+------------ 1 | Medical | 5 years (1 row) skills=# select * from qualifications ; st_id | sk_id | qu_qualified | qu_renewal -------+-------+--------------+------------ 1 | 1 | 2004-07-01 | 10 years 1 | 1 | 2009-05-25 | 3 years (2 rows) skills=# What is the best (cleanest SQL or fastest performance) way to produce the following view? st_id | st_name | sk_id | sk_desc | last_qualified | Renewal | Expires -------+---------+-------+---------+----------------|---------|----------- 1 | Gary | 1 | Medical | 2009-05-25 | 3 years | 2012-05-25 I've got the following which gives all but the last two fields. The problem is that the Renewal period and expires has to be from the most recent record, i.e. even though the record 1 above expires after record 2, the results of record 2 have to be used. select t.*, k.sk_id, k.sk_desc, q.last_qualified from (select st_id, sk_id, max(qu_qualified) as last_qualified from qualifications group by st_id, sk_id) q join staff t on t.st_id = q.st_id join skills k on k.sk_id = q.sk_id order by st_id, sk_id I am still at the concept stage for this project so I can change the schema if required -- Gary Stainburn Group I.T. Manager Ringways Garages http://www.ringways.co.uk