Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mIrmC-0003jQ-Ps for pgsql-novice@arkaria.postgresql.org; Wed, 25 Aug 2021 12:13:04 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1mIrmA-0003kA-OZ for pgsql-novice@arkaria.postgresql.org; Wed, 25 Aug 2021 12:13:02 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mIqkm-0003nz-SW for pgsql-novice@lists.postgresql.org; Wed, 25 Aug 2021 11:07:32 +0000 Received: from esa03.ucs.mun.ca ([134.153.136.23]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mIqkc-00025g-Qx for pgsql-novice@postgresql.org; Wed, 25 Aug 2021 11:07:29 +0000 IronPort-SDR: X2d3py0EWPYp6PW8cMFnn8lKbJzfy9xtqtr/94XHQbZzmnt5dncTPOsRicuahnz+wGEvf0CW7X GBq4N2sLz2e+Wc/SU/AYffS+BUsFmkUinN66nRLyw2UwFrpshjiWGvAPyHiDOVAD9ocuagmbRX EGBS1zpXkbJstk3M+5UO2IHRNtlHvvBaWdRFLu4PN8uWw1ubLocd6MLY7Dr+XdA/UOC9w1GHSE b+PckWiMwZ4hH6RX3D8gYTKwk0Y2/QHlBYt5d2W7KUKgiECEAxalzoUen+a18z5Hz/WgjgnWCF lzk= IronPort-HdrOrdr: =?us-ascii?q?A9a23=3AUb+zv69+wjbSe3CrDctuk+AEI+orL9Y04l?= =?us-ascii?q?Q7vn2ZKCYlCfBw8vrEoB1173HJYVoqNU3I2urwXJVoOEm8yXct2+ks1NSZLW?= =?us-ascii?q?vbUQmTXflfBOLZqlWLJ8SZzJ856U4KScdD4bPLYWSSwvyKhzVQvuxQpuWv4e?= =?us-ascii?q?SDv8+b5XFoVARrY6Zr40NCDBqBGEEefngkOXN8Luvk2vZ6?= X-IronPort-AV: E=Sophos;i="5.84,350,1620700200"; d="scan'208";a="58797263" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from cpe00fc8db7a323-cm00fc8db7a320.cpe.net.cable.rogers.com (HELO pyrope.local) ([174.117.202.255]) by smtp03.ucs.mun.ca with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Aug 2021 08:37:20 -0330 User-agent: mu4e 1.5.6; emacs 27.2 From: Roger Mason To: pgsql-novice Subject: select from grouped data Date: Wed, 25 Aug 2021 08:37:15 -0230 Message-ID: MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello, I have written this function to process some multiline text data into a table with both text & numeric columns: CREATE OR REPLACE FUNCTION get_final_energy () RETURNS TABLE ( jid text, "timestamp" text, scf integer, energy double precision ) AS $$ WITH a AS ( SELECT jid, regexp_split_to_table(totenergy_out, '\n') AS teo FROM results ), b AS ( SELECT results.jid, results. "timestamp", cast( CASE WHEN split_part(a.teo, ' ', 2) = '' THEN '0' ELSE split_part(a.teo, ' ', 2) END AS integer) AS scf, cast( CASE WHEN split_part(a.teo, ' ', 3) = '' THEN '0.0' ELSE split_part(a.teo, ' ', 3) END AS double precision) AS energy FROM results, a WHERE results.jid = a.jid GROUP BY results.jid, results. "timestamp", a.teo --HAVING -- scf = max(scf) ORDER BY timestamp ASC, scf DESC ) SELECT * FROM b; $$ LANGUAGE sql; The output looks like: jid | timestamp | scf | energy ------------+-----------------+-----+---------------- 1250_1 | 20210805-114634 | 18 | -1316.43700819 1250_1 | 20210805-114634 | 17 | -1316.43700825 1250_1 | 20210805-114634 | 16 | -1316.4370097 1250_1 | 20210805-114634 | 15 | -1316.43700991 1250_1 | 20210805-114634 | 14 | -1316.43699775 1250_1 | 20210805-114634 | 13 | -1316.43699117 1250_1 | 20210805-114634 | 12 | -1316.43750771 1250_1 | 20210805-114634 | 11 | -1316.43805358 1250_1 | 20210805-114634 | 10 | -1316.43857192 1250_1 | 20210805-114634 | 9 | -1316.43070942 1251_1 | 20210806-062539 | 18 | -1316.43700819 1251_1 | 20210806-062539 | 17 | -1316.43700826 .... What I want is to get (for each group) the energy corresponding to the maximum value of scf. I appreciate any help, Thanks, Roger