Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d7W3V-0001Rc-Qi for pgsql-sql@arkaria.postgresql.org; Sun, 07 May 2017 23:57:37 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1d7W3U-00037W-Qh for pgsql-sql@arkaria.postgresql.org; Sun, 07 May 2017 23:57:36 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1d7W3U-00033g-2E for pgsql-sql@postgresql.org; Sun, 07 May 2017 23:57:36 +0000 Received: from maildrop2.anu.edu.au ([130.56.64.108]) by magus.postgresql.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1d7W3P-0005Q8-2N for pgsql-sql@postgresql.org; Sun, 07 May 2017 23:57:34 +0000 Received: from mailpmx4.anu.edu.au (snatpool01-2.anu.edu.au [130.56.66.106]) by maildrop2.anu.edu.au (8.13.8/8.13.8) with ESMTP id v47NvNOD000367 for ; Mon, 8 May 2017 09:57:23 +1000 Received: from mailpmx4.anu.edu.au (localhost [127.0.0.1]) by localhost (Postfix) with SMTP id 8B20D80A03 for ; Mon, 8 May 2017 09:57:23 +1000 (AEST) Received: from smtphost.anu.edu.au (snatpool01-2.anu.edu.au [130.56.66.106]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by mailpmx4.anu.edu.au (Postfix) with ESMTPS id 4EEFF8037D for ; Mon, 8 May 2017 09:57:22 +1000 (AEST) Received: from [150.203.211.229] (littleblue.cecs.anu.edu.au [150.203.211.229]) by smtphost.anu.edu.au (8.13.8/8.13.8) with ESMTP id v47NvMec016609 for ; Mon, 8 May 2017 09:57:22 +1000 Subject: Re: Most recent row To: pgsql-sql@postgresql.org References: <201705050925.04194.gary.stainburn@ringways.co.uk> <201705051044.54259.gary.stainburn@ringways.co.uk> <20170505100037.GA23734@depesz.com> <201705051154.54863.gary.stainburn@ringways.co.uk> From: Bob Edwards Message-ID: <5e0382ea-163a-8d1c-5e4e-2afbb36c5888@cs.anu.edu.au> Date: Mon, 8 May 2017 09:57:22 +1000 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.6.0 MIME-Version: 1.0 In-Reply-To: <201705051154.54863.gary.stainburn@ringways.co.uk> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-PMX-Version: 6.3.3.2656215, Antispam-Engine: 2.7.2.2107409, Antispam-Data: 2017.5.7.234815, AntiVirus-Engine: 5.36.0, AntiVirus-Data: 2017.5.7.5360004 internal X-OriginatorOrg: anu.edu.au X-PerlMx-Spam: Gauge=IIIIIIII, Probability=8%, Report=' RCVD_FROM_IP_DATE 0.1, HTML_00_01 0.05, HTML_00_10 0.05, BODYTEXTP_SIZE_3000_LESS 0, BODY_SIZE_2000_2999 0, BODY_SIZE_5000_LESS 0, BODY_SIZE_7000_LESS 0, FROM_EDU_TLD 0, IN_REP_TO 0, LEGITIMATE_SIGNS 0, MSG_THREAD 0, NO_CTA_URI_FOUND 0, NO_URI_FOUND 0, NO_URI_HTTPS 0, REFERENCES 0, __BOUNCE_CHALLENGE_SUBJ 0, __BOUNCE_NDR_SUBJ_EXEMPT 0, __CT 0, __CTE 0, __CT_TEXT_PLAIN 0, __FORWARDED_MSG 0, __FRAUD_MONEY_BIG_COIN 0, __FRAUD_MONEY_BIG_COIN_DIG 0, __HAS_FROM 0, __HAS_MSGID 0, __IN_REP_TO 0, __MIME_TEXT_ONLY 0, __MIME_TEXT_P 0, __MIME_TEXT_P1 0, __MIME_VERSION 0, __MOZILLA_USER_AGENT 0, __NO_HTML_TAG_RAW 0, __PHISH_SPEAR_STRUCTURE_1 0, __REFERENCES 0, __SANE_MSGID 0, __SUBJ_ALPHA_END 0, __SUBJ_ALPHA_NEGATE 0, __TO_MALFORMED_2 0, __TO_NO_NAME 0, __USER_AGENT 0' X-Pg-Spam-Score: -4.2 (----) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-sql Precedence: bulk Sender: pgsql-sql-owner@postgresql.org On 05/05/17 20:54, Gary Stainburn wrote: > On Friday 05 May 2017 11:00:37 hubert depesz lubaczewski wrote: >> On Fri, May 05, 2017 at 10:44:54AM +0100, Gary Stainburn wrote: >>>> How many rows are in people? How many in assessments? Do you really >>>> want data on all people? Or just some? >>> >>> I was looking for a balanced solution, combining performance and SQL >>> 'purity'. >> >> Solution depends on answers to questions I asked. Will you always get >> data for all people? Or just some? How many assessments per person will >> there usually be? >> >> Best regards, >> >> depesz > > Oddly, part of my reply was missing. > > As I said, there are currently 720 rows in the people table. > The will be a new assessment record for each person roughly every six months. > The dataset will be open ended so will slowly but indefinitely grow. > > The part that was missing was that there will be new people records added who > initially will not have an assessment. They will still need to be included in > the report. > > > Depending upon what you might want to do with assessments that are not "most-recent", you could use a trigger function to move "out-of-date" assessments into a separate table and only keep the most recent as the "current" assessment in the assessment table. Makes the joins a little simpler and less time-consuming, again, depending upon what you want to do with non-most-recent rows. I have a real-world application that has marks awarded to assessments per student (>10k students, >1k assessments) and I keep two tables: 'mark' for "current" (or most recent) marks and 'old_mark' as a kind of audit trail of marks that have been modified, by whom, when and for what reason. I use two trigger functions: 'insert_mark_func' and 'update_mark_func' which are triggered on inserts and updates on the mark table respectively. Deletes are not allowed, but we can update an existing mark so that its value is "NULL", which gets treated as if it were a non-mark. Don't know if this is best-practice, and certainly not "pure SQL", but it is a balanced solution and has been working well for us for over 15 years. cheers, Bob Edwards. -- Sent via pgsql-sql mailing list (pgsql-sql@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-sql