pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Chuck Martin <clmartin@theombudsman.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: Query help
Date: Sun, 27 Jan 2019 14:12:44 -0800
Message-ID: <b6e3ca74-2ff3-0c0f-ee14-5b7b8178e75a@aklaver.com> (raw)
In-Reply-To: <CAFw6=U3PzZ8NpZ0N-Btn0jGaAr0x82kPEna0MOdVsCqkNT7stw@mail.gmail.com>
References: <CAFw6=U06QsBOBLNyj1ZWz0rAENLXemsQtprsZHYfuD=HED8xag@mail.gmail.com>
	<97b3bcff-8e3a-00a7-09bc-c6f82a420196@aklaver.com>
	<CAFw6=U3PzZ8NpZ0N-Btn0jGaAr0x82kPEna0MOdVsCqkNT7stw@mail.gmail.com>

On 1/27/19 1:50 PM, Chuck Martin wrote:
> 
> Chuck Martin
> Avondale Software
> 
> 
> On Sun, Jan 27, 2019 at 2:55 PM Adrian Klaver <adrian.klaver@aklaver.com 
> <mailto:adrian.klaver@aklaver.com>> wrote:
> 
>     On 1/26/19 3:04 PM, Chuck Martin wrote:
>     [snip]
>     Outline form:
> 
>     1) If a record is in ombcase it has a status('in a status') by
>     definition.
> 
>       From query below you are not looking for just records in ombcase, but
>     those that have a statusid other then 'closed%' in status table.
> 
>     2) For the criteria in 1) you want to find the age of the last
>     statuschange.
> 
>     To me that leads to something like:
> 
>     SELECT
>              case_pkey
>     FROM
>              ombcase AS
>     JOIN
>              status
>     ON
>              ombcase.case_pkey = status.status_fkey
>     LEFT JOIN
>              statuschange
>     ON  -- Or statuschange.ombcase_fkey. Not clear from above.
>              statuschange.case_fkey = ombcase.status_pkey
>     GROUP BY
>              ombcase.pkey
>     HAVING
>              status.LOWER(statusid) NOT LIKE ('closed%')
>     AND
>              max(coalesce(statuschange.insdatetime, ombcase.insdatetime))
>              < 'some date'
> 
>     Obviously not tested.
> 
> 
> Thanks, Adrian. This got me a lot closer, but I'm puzzled by the number 
> of records returned. There are 3120 ombcase records with a statusid that 
> is <> 'closed%'

> 
> SELECT count(ombcase.case_pkey)
> 
> FROM ombcase,status
> 
> WHERE ombcase.status_fkey = status.status_pkey  AND 
> lower(status.statusid) NOT LIKE  ('closed%')

To get an apples to apples comparison what does below return?:

SELECT  count(ombcase.case_pkey)
FROM ombcase

	INNER JOIN status

	ON ombcase.status_fkey = status.status_pkey

		LEFT JOIN statuschange

		ON statuschange.case_fkey = ombcase.case_pkey
AND
	LOWER(status.statusid) NOT LIKE ('closed%')


Best guess is the 258 records are the ombcase records that have no 
statuschange records, brought in by the LEFT JOIN.

> 
> 
> But 3378 are returned by:
> 
> SELECT  ombcase.case_pkey, ombcase.casename, 
> COALESCE(AGE(statuschange.insdatetime), AGE(ombcase.insdatetime)) AS 
> age_in_status
> 
> FROM ombcase
> 
> INNER JOIN status
> 
> ON ombcase.status_fkey = status.status_pkey
> 
> LEFT JOIN statuschange
> 
> ON statuschange.case_fkey = ombcase.case_pkey
> 
> GROUP BY ombcase.case_pkey, status.statusid, statuschange.insdatetime, 
> ombcase.insdatetime
> 
> HAVING LOWER(status.statusid) NOT LIKE ('closed%')
> 
> AND ombcase.case_pkey <> 0
> 
> AND MAX(COALESCE(AGE(statuschange.insdatetime), 
> AGE(ombcase.insdatetime))) > '2 months'
> 
> ORDER BY age_in_status DESC
> 
> I don't know where the extra 258 records came from, and I think I need 
> to keep working on it until the query returns 3120 records.


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (70+ messages)  latest in thread

Message-ID: <b6e3ca74-2ff3-0c0f-ee14-5b7b8178e75a@aklaver.com>
Permalink:  ../b6e3ca74-2ff3-0c0f-ee14-5b7b8178e75a@aklaver.com/
Also on:    postgresql.org/message-id/b6e3ca74-2ff3-0c0f-ee14-5b7b8178e75a@aklaver.com

 · 

reply

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: adrian.klaver@aklaver.com, clmartin@theombudsman.com
  Subject: Re: Query help
  In-Reply-To: <b6e3ca74-2ff3-0c0f-ee14-5b7b8178e75a@aklaver.com>
  References: <CAFw6=U06QsBOBLNyj1ZWz0rAENLXemsQtprsZHYfuD=HED8xag@mail.gmail.com> <97b3bcff-8e3a-00a7-09bc-c6f82a420196@aklaver.com> <CAFw6=U3PzZ8NpZ0N-Btn0jGaAr0x82kPEna0MOdVsCqkNT7stw@mail.gmail.com> <b6e3ca74-2ff3-0c0f-ee14-5b7b8178e75a@aklaver.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