客户提出的产品数量要求

时间:2016-12-29 04:35:33

标签: sql oracle oracle10g

我有两个表客户请求和库存表

stock_available

$term = $_GET['mysearch'];

customer_request

itemid  itemname  stock
P01     phone     200

我需要用orderno Cid Cname itemid qty dt 01 C01 vasu p01 100 28-dec-2016 02 C02 mahh p01 80 28-dec-2016 03 c03 gggg p01 50 29-dec-2016 编写查询。 itemid的数量之和不会超过首先出现的dt的股票和订单

必填结果:

cid,itemid,stock,qty,dt,orderno

2 个答案:

答案 0 :(得分:1)

试试这个:

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      |
+---------+-----+---------+--------+-----+-----------+---------+

答案 1 :(得分:0)

select * from 
stock_available s , 
customer_request cr 
where s.itemid = cr.itemid
order by cr.dt asc, cr.qty desc;