Clean up shop database

From: Richard Klingler <richard(at)klingler(dot)net>
To: pgsql-sql(at)lists(dot)postgresql(dot)org
Subject: Clean up shop database
Date: 2022-01-19 11:03:17
Message-ID: 20220119120317479683.54287f97@klingler.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-sql

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

Responses

Browse pgsql-sql by date

  From Date Subject
Next Message Rob Sargent 2022-01-19 12:04:09 Re: Clean up shop database
Previous Message Steve Midgley 2022-01-05 16:42:55 Re: Return product category with hierarchical info