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 1gnk9n-0004j2-9a for pgsql-general@arkaria.postgresql.org; Sun, 27 Jan 2019 13:07:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gnk9l-0000SJ-VG for pgsql-general@arkaria.postgresql.org; Sun, 27 Jan 2019 13:07:25 +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 1gnk9l-0000SC-NI for pgsql-general@lists.postgresql.org; Sun, 27 Jan 2019 13:07:25 +0000 Received: from mail.hjp.at ([212.17.106.138] helo=rorschach.hjp.at) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gnk9e-0004RM-D8 for pgsql-general@lists.postgresql.org; Sun, 27 Jan 2019 13:07:25 +0000 Received: by rorschach.hjp.at (Postfix, from userid 1000) id 335DB2463; Sun, 27 Jan 2019 14:07:16 +0100 (CET) Date: Sun, 27 Jan 2019 14:07:16 +0100 From: "Peter J. Holzer" To: pgsql-general@lists.postgresql.org Subject: Re: Query help Message-ID: <20190127130716.44btztfrenqbwueb@hjp.at> References: MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="ctzfzvjzlvpr3ier" Content-Disposition: inline In-Reply-To: User-Agent: NeoMutt/20170113 (1.7.2) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --ctzfzvjzlvpr3ier Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On 2019-01-26 18:04:23 -0500, Chuck Martin wrote: > I'm having trouble formulating a query. This is a simplified version of t= he > tables: >=20 > ombcase > ------------ > case_pkey integer, primary key > casename varchar > insdatetime timestamp w/o time zone > status_fkey integer, foreign key >=20 > status > -------- > status_pkey integer, primary key > statusid varchar >=20 > 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 >=20 > 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).=A0 >=20 > 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= =2E=A0 The easiest way is to use set operations: select case_pkey from ombcase; gives you all the ombcase ids. select ombcase_fkey from statuschange where insdatetime >=3D now()::date - = xx; gives you all ombcase ids which had a status change in the last xx days. Therefore, select case_pkey from ombcase except select ombcase_fkey from statuschange where insdatetime >=3D now()::date - = xx; gives you all ombcase ids which did /not/ have a status change in the last xx days. Another way would be to use a CTE (https://www.postgresql.org/docs/10/queries-with.html) to extract the last status change for each ombcase and then do a left join of ombcase to that CTE. hp --=20 _ | Peter J. Holzer | we build much bigger, better disasters now |_|_) | | because we have much more sophisticated | | | hjp@hjp.at | management tools. __/ | http://www.hjp.at/ | -- Ross Anderson --ctzfzvjzlvpr3ier Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCAAdFiEETtJbRjyPwVTYGJ5k8g5IURL+KF0FAlxNrP8ACgkQ8g5IURL+ KF2hNA//QukAEUVJCQiWpa/38bm4ZixkGTnBglFDuIJBa47UoydszZGTv6kuMnw7 f0l1OhVuMNkIAIeTYRW6R6SYJ9m7+lYzX8R0yca/ffslXdGpAbsK2hf6Ss0+IO5a AI/HdUEzn3vjC5omGpEm1YdGebi5Onze9m+cXmUzTeVRvk1DlYCTnaR8CaXrjX7O HwsQDnNWmgPbwukNn8Giuik0VA6Ge8Du8k+IHdQHxehIHLaGHmrXT48DeOEuVQqT 2dHnKWDkcDUvu2q/S3FH958hyDvMqWJ64u8Ald9XQCX7qimmNWNfIgrVA9W/Ww+0 PrmZARi7ou1Y7JBX98v161CmHrQ0ezHTkZWpHeATO1Xsc13Yt91nPxJfqDTLtxep 6yb49CbBXv3ah9y0epidZeIFE4T3Wh3xvtIGfGy8ok+T2jI0eYkec6Qfvp6SRgb6 bq4OYAapVb/ReWS6Knrq/jI8X205+YekfhDqYtcpVOJN3LAvyoYI7JaOO/3VnvSn IKUKculXKrwzquoWTrLIeG0DC32kA4Nx/lCRvyWFqZVokG4g5m1r6mxjeAMXu4Ox chyEbKFcUEXHzUc6E4UhxnQV+yPUGLFwM7Gb98XlHJ5FYDZlX9YgZW1wpM7XfqkA Yag2iMVKotztveyUWOHcMQ2uzJWAWQkQbVIuj8+8PzAzZqHPDnA= =bAzZ -----END PGP SIGNATURE----- --ctzfzvjzlvpr3ier--