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 1lNXjq-0005bf-VD for pgsql-general@arkaria.postgresql.org; Sat, 20 Mar 2021 09:17:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1lNW4r-0006x4-RE for pgsql-general@arkaria.postgresql.org; Sat, 20 Mar 2021 07:31:17 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNW4r-0006wx-Jd for pgsql-general@lists.postgresql.org; Sat, 20 Mar 2021 07:31:17 +0000 Received: from securemail-y8.synaq.com ([196.35.198.43]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lNW4p-0004cb-1y for pgsql-general@lists.postgresql.org; Sat, 20 Mar 2021 07:31:17 +0000 Authentication-Results: synaq.com; iprev=fail smtp.remote-ip=197.90.32.26; auth=pass (PLAIN) smtp.auth=frank@chagford.com; arc=none Received: from [197.90.32.26] (helo=[192.168.0.8]) by securemail-pl-omx8.synaq.com with esmtpa (Exim 4.93.0.4-30-5edf9c7) (envelope-from ) id 1lNW4G-000m3S-9S for pgsql-general@lists.postgresql.org; Sat, 20 Mar 2021 09:30:40 +0200 Subject: Re: SELECT is faster on SQL Server To: pgsql-general@lists.postgresql.org References: <33b19e80-75da-a455-7e78-a01e14e4d4d6@gmx.net> <47da8273-23dd-789a-ca7d-a5b803d9010f@gmx.net> From: Frank Millman Message-ID: Date: Sat, 20 Mar 2021 09:30:25 +0200 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: <47da8273-23dd-789a-ca7d-a5b803d9010f@gmx.net> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US X-SYNAQ-Pinpoint-Information: Please contact SYNAQ for more information X-SYNAQ-Pinpoint-ID: 1lNW4G-000m3S-9S X-SYNAQ-Pinpoint: No virus infections found X-SYNAQ-Pinpoint-SpamCheck: not spam, SpamAssassin (not cached, score=-1.801, required 9, autolearn=disabled, ALL_TRUSTED -1.00, DCC_REPUT_00_12 -0.80, NICE_REPLY_A -0.00) X-Pinpoint-From: frank@chagford.com List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2021-03-19 7:11 PM, Thomas Kellerer wrote: > Frank Millman schrieb am 19.03.2021 um 10:16: >> >> cl_bal selects WHERE tran_date <= '2018-03-31'. >> >> op_bal selects WHERE tran_date < '2018-03-01'. >> >> The second one could be written as WHERE tran_date <= '2018-02-28', >> but I don't think that would make any difference. > > I knew I overlooked something ;) > > But as one is a true subset of the other, I think you can merge that > into a single SELECT statement: > >     select '2018-03-01' AS op_date, >            '2018-03-31' AS cl_date, >            a.source_code_id, >            sum(a.tran_tot) AS cl_tot, >            sum(a.tran_tot) filter (where tran_date < '2018-03-01') AS > op_tot >     FROM ( >        SELECT distinct on (location_row_id, function_row_id, > source_code_id) source_code_id, tran_tot, tran_date >        FROM prop.ar_totals >        WHERE deleted_id = 0 >          AND tran_date <= '2018-03-31' >          AND ledger_row_id = 1 >        ORDER BY location_row_id, function_row_id, source_code_id, > tran_date DESC >     ) AS a >     GROUP BY a.source_code_id > Thanks very much Thomas - I did not know about FILTER. But it does not quite work. If the SELECT does find a row where the max tran_date is <= '2018-03-31' it correctly includes it in 'cl_tot'. But the filter returns nothing for 'op_tot' because there is no corresponding row where tran_date < '2018-03-01'. But I have learned something new, so thanks for that. Frank