Попробуйте это:
SELECT orderno, cid, cname, itemid, qty, dt,
CASE WHEN s > 0 THEN s ELSE 0 END - LAG(CASE WHEN s > 0 THEN s ELSE 0 END,1,0) over (partition by itemid order by orderno) pending
FROM
(SELECT c.*,
SUM(c.qty) over (partition BY c.itemid order by c.dt, c.orderno) - s.stock s
FROM stock_available s
INNER JOIN customer_request c
ON s.itemid = c.itemid
);
Тест 1:
select * from stock_available;
+--------+----------+-------+
| ITEMID | ITEMNAME | STOCK |
+--------+----------+-------+
| P01 | phone | 200 |
+--------+----------+-------+
select * from customer_request;
+---------+-----+-------+--------+-----+-----------+
| ORDERNO | CID | CNAME | ITEMID | QTY | DT |
+---------+-----+-------+--------+-----+-----------+
| 1 | C01 | vasu | P01 | 100 | 28-DEC-16 |
+---------+-----+-------+--------+-----+-----------+
| 2 | C02 | mahh | P01 | 80 | 28-DEC-16 |
+---------+-----+-------+--------+-----+-----------+
| 3 | C03 | gggg | P01 | 50 | 29-DEC-16 |
+---------+-----+-------+--------+-----+-----------+
Результат моего запроса:
+---------+-----+-------+--------+-----+-----------+---------+
| ORDERNO | CID | CNAME | ITEMID | QTY | DT | PENDING |
+---------+-----+-------+--------+-----+-----------+---------+
| 1 | C01 | vasu | P01 | 100 | 28-DEC-16 | 0 |
+---------+-----+-------+--------+-----+-----------+---------+
| 2 | C02 | mahh | P01 | 80 | 28-DEC-16 | 0 |
+---------+-----+-------+--------+-----+-----------+---------+
| 3 | C03 | gggg | P01 | 50 | 29-DEC-16 | 30 |
+---------+-----+-------+--------+-----+-----------+---------+
Тест 2:
-- Added one more request
insert into customer_request values(4, 'C04','some','P01',400,'30-Dec-2016');
+---------+-----+-------+--------+-----+-----------+---------+
| ORDERNO | CID | CNAME | ITEMID | QTY | DT | PENDING |
+---------+-----+-------+--------+-----+-----------+---------+
| 1 | C01 | vasu | P01 | 100 | 28-DEC-16 | 0 |
+---------+-----+-------+--------+-----+-----------+---------+
| 2 | C02 | mahh | P01 | 80 | 28-DEC-16 | 0 |
+---------+-----+-------+--------+-----+-----------+---------+
| 3 | C03 | gggg | P01 | 50 | 29-DEC-16 | 30 |
+---------+-----+-------+--------+-----+-----------+---------+
| 4 | C04 | some | P01 | 400 | 30-DEC-16 | 400 |
+---------+-----+-------+--------+-----+-----------+---------+
Тест 3:
insert into stock_available values ('P02','something else',10);
insert into customer_request values(5, 'C05','someone','P02',50,'30-Dec-2016');
+---------+-----+---------+--------+-----+-----------+---------+
| ORDERNO | CID | CNAME | ITEMID | QTY | DT | PENDING |
+---------+-----+---------+--------+-----+-----------+---------+
| 1 | C01 | vasu | P01 | 100 | 28-DEC-16 | 0 |
+---------+-----+---------+--------+-----+-----------+---------+
| 2 | C02 | mahh | P01 | 80 | 28-DEC-16 | 0 |
+---------+-----+---------+--------+-----+-----------+---------+
| 3 | C03 | gggg | P01 | 50 | 29-DEC-16 | 30 |
+---------+-----+---------+--------+-----+-----------+---------+
| 4 | C04 | some | P01 | 400 | 30-DEC-16 | 400 |
+---------+-----+---------+--------+-----+-----------+---------+
| 5 | C05 | someone | P02 | 50 | 30-DEC-16 | 40 |
+---------+-----+---------+--------+-----+-----------+---------+
как же появляются 3 строки? 100 + 80 + 50> 200 .. – coderredoc
Таблица Customer_request имеет 3 запроса всего 230. В нашей таблице акций у нас есть 200 очков, мы должны отдавать приоритет FIFO. Превышение 30 будет выпущено позже. –
, но какова будет путёт вашего запроса? – coderredoc