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 1gnqWw-0008VJ-Rf for pgsql-general@arkaria.postgresql.org; Sun, 27 Jan 2019 19:55:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gnqWv-0006Xv-M0 for pgsql-general@arkaria.postgresql.org; Sun, 27 Jan 2019 19:55:45 +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 1gnqWu-0006XV-5D for pgsql-general@lists.postgresql.org; Sun, 27 Jan 2019 19:55:45 +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 1gnqWq-0005Ag-Be for pgsql-general@postgresql.org; Sun, 27 Jan 2019 19:55:43 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 7733421CA5; Sun, 27 Jan 2019 14:55:38 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Sun, 27 Jan 2019 14:55:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= from:subject:to:references:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=/ 6hDPoVqsQYExBZzm+CipeaNy8o+sayCl/emA8tTnDE=; b=mkjbIoeLSYtzCaLZx 2QF0V3dxDMxtWBsc9sLjjsmbN3l97G8ZkqgJ6axttjvM4kWroOKjqB+WRTbrufhT zzdIHP0YoyQugE7XC79t4KHgZT0GBAhywzDb3cl4Tpdjo8UIjOyiqeXsFc4E+wGK YqeY3j0HjagpnEY7bguij1ND1yDZ5mF96+XeIP0XZ30vGsT6ZAKillp9EsXhuCbs ILvC12ksebxwO+K/3yho4LsS/MlkTUj+1hx5bWTAIZmxDrEohVhZtr6JI3sVT2hd ev8bvm9K4YR4PxEb/MVHRQ5/o+9+w0+Ui1aFibX6RdQmmSst8QodpZtLpXWC663p W9isA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=/6hDPoVqsQYExBZzm+CipeaNy8o+sayCl/emA8tTn DE=; b=rO0xbn0Oji92rmTa2/vdlf+s37WGesv9t9S0YQCfOxX7sJuYQvi9UtG19 zaBckfTdIwoKIzS9mEl/ApDOwwQ96dXhuSxgDgbCzz1hPjCZSFDKkXnQT+4rRQzp jiSB5yOkb7C+oWQ7hnPgbgIhDGEz9CZBazHdvlxdbkssw73tWjJPEDePVj8WeCTw yIE1qFi8rqqkcmDBJaGK/psGnXx5tgVC+xWZd6M0ZWHTMkwgbDCd6dgWgShajsiO sFOgSMpK/n3RlGDiApRAuQvI5LteuwSHqO5mXD7CcGPgWiUt5Prb5nd8RbZobWoD nvnQlJyXrRxeGoB+xcRdX9jpjbi9Q== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtledrieekgddufedvucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfquhhtnecuuegrihhlohhuthemucef tddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefhuffvfhfkff 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 8EAF310087; Sun, 27 Jan 2019 14:55:37 -0500 (EST) From: Adrian Klaver Subject: Re: Query help To: Chuck Martin , pgsql-general References: Message-ID: <97b3bcff-8e3a-00a7-09bc-c6f82a420196@aklaver.com> Date: Sun, 27 Jan 2019 11:55:36 -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/26/19 3:04 PM, Chuck Martin wrote: > I'm having trouble formulating a query. This is a simplified version of > the tables: > > ombcase > ------------ > case_pkey integer, primary key > casename varchar > insdatetime timestamp w/o time zone > status_fkey integer, foreign key > > status > -------- > status_pkey integer, primary key > statusid varchar > > statuschange > -------- > statuschange_pkey integer, primary key > insdatetime timestamp w/o time zone > ombcase_fkey integer, foreign key > oldstatus_fkey integer, foreign key > newstatus_fkey integer, foreign key > active integer, not nullable > > The idea should be obvious, but to explain, insdatetime is set when a > new record is created in any table. All records in ombcase have a > foreign key to status that can't be null. When status changes, a record > is created in statuschange recording the old and new status keys, and > the time (etc). > > The goal is to find records in ombcase that have not had a status change > in xx days. If the status has not changed, there will be no statuschange > record. > > This query returns the age of each ombcase and the last statuschange > record, but only if there is a statuschange record: > > --Finds the age and last status change for open cases, but not age of > cases with no status change > > SELECT casename, age(ombcase.insdatetime) AS caseage, > age(laststatuschange.created_at) AS statusage > > FROM > > (SELECT > > case_fkey, MAX(insdatetime) AS created_at > > FROM > > statuschange > > GROUP BY > > case_fkey) AS laststatuschange > > INNER JOIN > > ombcase > > ON > > laststatuschange.case_fkey = case_pkey > > RIGHT JOIN status > > ON status_fkey = status_pkey > > WHERE lower(statusid) NOT LIKE ('closed%') > > AND case_pkey <> 0 > > > I want to use coalesce > (age(ombcase.insdatetime),age(statuschange.insdatetime) ) to return the > time that a case has been in a status—or without a status change. But > first I have to find the cases with no statuschange record. I was able > to do that, too, using this query: 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. > > > --find cases in status too long > > SELECT  casename, coalesce > (age(ombcase.insdatetime),age(statuschange.insdatetime) ) > > FROM ombcase > > LEFT JOIN statuschange > > ON case_fkey = case_pkey > > LEFT JOIN status > > ON status_fkey = status_pkey > > AND lower(statusid) NOT LIKE ('closed%') > > AND coalesce ( age(ombcase.insdatetime), age(statuschange.insdatetime) ) > > '2 months' > > > But this query will return all statuschange records for an ombcase > record that has multiple ones. > > > Any suggestions on how to combine the two ideas? > > > Chuck Martin > Avondale Software > -- > Chuck Martin > Avondale Software -- Adrian Klaver adrian.klaver@aklaver.com