How to distribute products to shop by amount of sales

From: Andrus <kobruleht2(at)hot(dot)ee>
To: pgsql-general(at)postgresql(dot)org
Subject: How to distribute products to shop by amount of sales
Date: 2020-08-16 06:32:14
Message-ID: 6fc9d515-6117-beae-3726-501dd9c1711e@hot.ee
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi!

Table of product types  contains desired quantities in shop

    create temp table producttype (
     productype char(10) primary key,
     desired integer check ( desired> 0)
    ) on commit drop ;
    insert into producttype values ( 'SHOE',3);

product table contains products and product types

    create temp table product  (
    product char(20) primary key,
    producttype char(10) references producttype
    ) on commit drop;

    insert into product values
('SHOE1','SHOE'),('SHOE2','SHOE'),('SHOE3','SHOE'),('SHOE4','SHOE'),('SHOE5','SHOE');

Warehouse table contains quantities in warehouse to distribute

    create temp table warehouse (
      product char(20) primary key references product,
      quantity integer check ( quantity> 0)
    ) on commit drop ;
    insert into warehouse values ('SHOE1',50),('SHOE2',60),('SHOE3',70);

Shop table contains quantities in shop

    create temp table shop (
      product char(20) primary key references product,
      quantity integer check ( quantity> 0)
    ) on commit drop;
    insert into shop values ('SHOE4',1);
    insert into shop values ('SHOE5',1);

sales table contains sold quantities. Most sold intems should moved from
stock  first

    create temp table sales (
      product char(20) primary key references product,
      quantity integer check ( quantity> 0)
    ) on commit drop;
    insert into sales values ('SHOE1',100),('SHOE2',200);

How to find product which should moved from warehouse to shop so that
shop status will be increated to producttype.desired
quantity for products in warehouse ? Most sold products should moved first.
Only one product (quantity 1) should moved from each product code.

Using data abouve, there should be 3 shoes (producttype.desired) in shop
but are only 2 (sum(shop.quantity) for shoes).
Most sold shoe in warehouse is SHOE2
So SHOE2 should be moved to shop from warehouse.

How to find products which should moved ?
Can some SELECT with window function used for this ?

PostgreSQL 9.3.5 is used.

Andrus.

Posted also in
https://stackoverflow.com/questions/63433824/how-to-distribute-products-to-shop-by-amount-of-sales

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Ian Lawrence Barwick 2020-08-16 12:06:38 Re: How to distribute products to shop by amount of sales
Previous Message Stephen Frost 2020-08-15 19:17:03 Re: pg_basebackup + delta base backups