From: Frank Millman <frank@chagford.com>
To: pgsql-general@lists.postgresql.org
Subject: Re: SELECT is faster on SQL Server
Date: Sat, 20 Mar 2021 09:30:25 +0200
Message-ID: <a611d434-7dbf-384d-e8f4-c2b8d5c21440@chagford.com> (raw)
In-Reply-To: <47da8273-23dd-789a-ca7d-a5b803d9010f@gmx.net>
References: <d002a9e4-433c-3f44-90aa-46568db4bc00@chagford.com>
<33b19e80-75da-a455-7e78-a01e14e4d4d6@gmx.net>
<fbdd2bdd-1957-1b49-eeca-0278d43de71d@chagford.com>
<fdacd364-7203-b268-76d5-d763c0758964@gmx.net>
<f93dc024-6563-3ae2-e2af-44a723fdade1@chagford.com>
<47da8273-23dd-789a-ca7d-a5b803d9010f@gmx.net>
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-general@postgresql.org
Cc: frank@chagford.com
Subject: Re: SELECT is faster on SQL Server
In-Reply-To: <a611d434-7dbf-384d-e8f4-c2b8d5c21440@chagford.com>
References: <d002a9e4-433c-3f44-90aa-46568db4bc00@chagford.com> <33b19e80-75da-a455-7e78-a01e14e4d4d6@gmx.net> <fbdd2bdd-1957-1b49-eeca-0278d43de71d@chagford.com> <fdacd364-7203-b268-76d5-d763c0758964@gmx.net> <f93dc024-6563-3ae2-e2af-44a723fdade1@chagford.com> <47da8273-23dd-789a-ca7d-a5b803d9010f@gmx.net> <a611d434-7dbf-384d-e8f4-c2b8d5c21440@chagford.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox