Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nA8kU-0008WJ-5u for pgsql-sql@arkaria.postgresql.org; Wed, 19 Jan 2022 11:03:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nA8kS-0001K1-OB for pgsql-sql@arkaria.postgresql.org; Wed, 19 Jan 2022 11:03:28 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nA8kS-0001Jp-Da for pgsql-sql@lists.postgresql.org; Wed, 19 Jan 2022 11:03:28 +0000 Received: from mail.klingler.net ([2a02:c207:2032:8354::1]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nA8kM-00006D-QN for pgsql-sql@lists.postgresql.org; Wed, 19 Jan 2022 11:03:25 +0000 Date: Wed, 19 Jan 2022 12:03:17 +0100 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=klingler.net; s=2020; t=1642590199; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=kbjmQZL9/F875REetXuz39dwjzn5i9ffuffoWTF88VQ=; b=rD5TKfdBIQ+GxEuwZ9KlEwCPKeBfIJvFvcfKdC8C0MocnBAbzIhZu72OXVaQkZ+uiflj1b KpjzlqvA3RALI5ZfRtBkUyOJ7e0nN1igwzE2UwwZ6ngLTZESD5DYLELuIP8eQ2Qbgw9ZY5 lZ9NWiBDUJSklS+Fb1dxJYGA83TpQ4Q= From: Richard Klingler To: pgsql-sql@lists.postgresql.org Message-ID: <20220119120317479683.54287f97@klingler.net> Subject: Clean up shop database MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Content-Language: en-US Authentication-Results: ORIGINATING; auth=pass smtp.auth=richard@klingler.net smtp.mailfrom=richard@klingler.net List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Good morning (o; I am in the process of migrating an online shop to another system and therefore also want to clean out products that haven't been re-stocked for a time. Now this simple query returns all order ids younger than 750 days: select orderid, orderdate from orders where (now() - orderdate) < INTERVAL '1000 days' order by orderdate asc So it shows me orders beginning from January 1st 2020...all fine. Now I want to list all products which stock is 0 and have only been ordered before those 750 days..so I use the above query in wrap it in the select with a "not in": select p.productid as id, p.name_de as name from product p, orderitems i, orders where p.productid = i.orderitems2productid and i.orderitems2orderid not in (select orderid from orders where (now() - orderdate) < INTERVAL '750 days') and p.pieces < 1 and p.active = 't' group by id order by id desc Besides that this query takes over 70 seconds...it also returns products that have been ordered after January 1st 2020. So somehow this "not in" doesn't work as I am expecting it (o; thanks in advance richard