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 1nA9hO-0002n7-QD for pgsql-sql@arkaria.postgresql.org; Wed, 19 Jan 2022 12:04:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nA9hL-00006m-QD for pgsql-sql@arkaria.postgresql.org; Wed, 19 Jan 2022 12:04:19 +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 1nA9hL-00006c-Fn for pgsql-sql@lists.postgresql.org; Wed, 19 Jan 2022 12:04:19 +0000 Received: from mail-io1-xd36.google.com ([2607:f8b0:4864:20::d36]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1nA9hF-0000dU-6f for pgsql-sql@lists.postgresql.org; Wed, 19 Jan 2022 12:04:19 +0000 Received: by mail-io1-xd36.google.com with SMTP id o9so2522865iob.3 for ; Wed, 19 Jan 2022 04:04:12 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=message-id:date:mime-version:user-agent:subject:content-language:to :references:from:in-reply-to:content-transfer-encoding; bh=aCcMkbv6JY//Usj9DSLRqvY0qnpgFy+z45Rog3YIYIs=; b=cNpIilbfFZEFR/SCj02WLjzoNznc/slNV/9bVoh5rANVfLymretL93LqSIewIUOXpL mwLuggBvW1IipMd0nBr1RLLWYkMTizqT+RYKaNo8dd9dFMewwhqItIRTkVgxMCIY9kgz jX+DibVbFEgNsNqQ/Fy4BU9XwFZ2cEVw83XlacEv5wLUsYM+YxPNwW5oS1EjSMiSL2rE 1TBQYXaub3Tba8DgNA2yPBw+N/E6SwJLa6vHdGddfE1W14B2rgqbXsPsk3zsiY4xKqbC yk67u1U5iAF/V6l4IQTttILg9iSE2CwsqHH8sEsJxwfYN/a59q6uIfmL8RTyXB6S2OhX Hk8A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=aCcMkbv6JY//Usj9DSLRqvY0qnpgFy+z45Rog3YIYIs=; b=EgpF0993vTPLbDMeWzDFzOIY3U0mAcVYrskl+x62Igimke9NeWTG3LaAiIfcaRdPKl Ptax6JM8Kpx+/3VvVeiDmHnaXI/rf930W1GfDOR48knKT6C8IfKWk4AFKlVtXE2ND6di GjtVgzgfCVtgV7j5jM0Px11RxNfqJVRUd2cv4leMkkBdbioN3ZEHqixn5yUU+TK/4vsj R2Xo+SFGyTkDGsFtbg0sVvncddU/741cEy9IcteY3HbeNtCdbLQTFX3C6oizUl/O5wNh 8KjYGnptP+7zRlk+1Y5V4Fa8d3u3yohW1SXQaPOvgulRwXcegH9PgebSphOkm2YR+dEB ROvQ== X-Gm-Message-State: AOAM531wuoa6o9VF/+7uhYjXSZpUI5TLQeNG+yrjTkRjy+z1+1qtZkiQ nH+lE5A1hrx3dOgvuSdrwCcNFBO7+tY= X-Google-Smtp-Source: ABdhPJxafM1fzJi+xlkk4a0uSwo7oxownfZLYxD5L2VVo928lfrBqBHvQdqMtNB492mu30Z7M9Zd3A== X-Received: by 2002:a05:6638:3009:: with SMTP id r9mr13408375jak.262.1642593850913; Wed, 19 Jan 2022 04:04:10 -0800 (PST) Received: from [10.0.0.94] (c-73-63-59-118.hsd1.ut.comcast.net. [73.63.59.118]) by smtp.gmail.com with ESMTPSA id a11sm11878630ilj.33.2022.01.19.04.04.10 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 19 Jan 2022 04:04:10 -0800 (PST) Message-ID: <9713782b-9c29-2497-be30-6dd1af0d550c@gmail.com> Date: Wed, 19 Jan 2022 05:04:09 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.5.0 Subject: Re: Clean up shop database Content-Language: en-CA To: pgsql-sql@lists.postgresql.org References: <20220119120317479683.54287f97@klingler.net> From: Rob Sargent In-Reply-To: <20220119120317479683.54287f97@klingler.net> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 1/19/22 04:03, Richard Klingler wrote: > 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 something like this? select p.productid as id, p.name_de as name from product p join orderitems i on p.productid = i.orderitems2productid join orders o on i.orderid = o.orderid where o.orderdate < 'January 1st 2020' 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 > > >