Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gnsfb-0008Ky-5Y for pgsql-general@arkaria.postgresql.org; Sun, 27 Jan 2019 22:12:51 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gnsfZ-0003Df-7C for pgsql-general@arkaria.postgresql.org; Sun, 27 Jan 2019 22:12:49 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1gnsfY-0003DY-Vd for pgsql-general@lists.postgresql.org; Sun, 27 Jan 2019 22:12:49 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gnsfX-000881-0e for pgsql-general@postgresql.org; Sun, 27 Jan 2019 22:12:48 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 9772B20D12; Sun, 27 Jan 2019 17:12:45 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Sun, 27 Jan 2019 17:12:45 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=R Z8A62Rt8ISeJ/ZkTJQkAZUEKObmBeAaBNNoj5f4G+k=; b=SgWRqMvQFDC3aZuQJ 4Ww+HomPssd57v1VbWYw6TXc/56F8N01Wr6OqaEAuh3E3mRJZaItbqUIhgThp7vs fa9YNCavgf4udPrnSeCJQ4Sd7PvNca4rp16yCW47cqEsVlTQanJejY70MNfxFcKI L+D22oXvR4rdoDACrE5yB43RyGqy8JXt9sVOOCuYai+Hi8y/snXwOUike00otWLM FfxzOsJGIS4cIA1z8lS9GVPOOJrfVr8kZSHDY0mpji2SpSb5/7kRHseJUsDUJLs+ z9TnnewXP9PUW3aUPxevBqdwWdkn6Pzsb22hAlG0VpxU3oRl9Y33c1s+BZF2ruwG YPUEg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:from:in-reply-to:message-id:mime-version:references :subject:to:x-me-proxy:x-me-proxy:x-me-sender:x-me-sender :x-sasl-enc; s=fm1; bh=RZ8A62Rt8ISeJ/ZkTJQkAZUEKObmBeAaBNNoj5f4G +k=; b=LBL/StxSNmd3Ci+Cwg3VyqBXNN2uHfNrc+3I2z8mAINHSzsDWg8sQVzNK jLWWrEerjJtucVu4R0i1bzRam9RNknmHRhdhTcjQaFTrB6BFCx6J43OPozpArkQz 4G8RPMpSaMC/W9lrnzwpwD4wJdSD48CMz/JpjI/mYlg8ahr6dYX8dTdbHIpmGynt aVCSXK6D3YZ1o5VAEiLB7aNZa3G+zj4pDiTo4/ZPIUuARkx9gKYDnDOnK8KQWGdd YADQ03f+PFjTwa+2i3mmaRHBiZBunsr/aLLlMvRPeINM2xMfBkqIPSSvB0fdvPYi qvMM1mjA8Smvo2xmAlm+1ClDxhwvA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrieekgdduheelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfquhhtnecuuegrihhlohhuthemucef tddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefuvfhfhffkff gfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceo rggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecukfhppeejuddrvd duvddrudefgedrfeenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgr vhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.2] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id AC38A10281; Sun, 27 Jan 2019 17:12:44 -0500 (EST) Subject: Re: Query help To: Chuck Martin Cc: pgsql-general References: <97b3bcff-8e3a-00a7-09bc-c6f82a420196@aklaver.com> From: Adrian Klaver Message-ID: Date: Sun, 27 Jan 2019 14:12:44 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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 > 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