在 MySQL 按所选日期和仓库查询中显示库存的 INWARD 和 OUTWARD 库存数量

Display a INWARD & OUTWARD of Stock Quantity of Inventory in MySQL Query By Selected Date and Warehouse

在查询中需要帮助以获得所需的输出。这是我的 Table 结构:

  1. 产品

    这里 qty 是股票

    id product_name qty
    1 Test 5
  2. 购买

    从采购 table 开始,我们必须 select 通过 ID

    获取日期和仓库
    id warehouse id date
    1 1 04-03-2022
  3. 购买物品

    Inward Stock 应从这里显示 table

    id product_id qty purchase_id
    1 1 5 1
  4. 订单

    从订单 table,我们必须 select 按 ID 的日期和仓库 这里是仓库ID

    id detail date
    1 1 04-03-2022
  5. 订单商品

    Outward Stock 应该从这里显示table。

    id product_id qty order_id
    1 1 5 1

期望的输出应该是这样的:

其中 P 表示购买,O 表示订购

Product name Inward outward closing
Test(P) 5 5
Test(O) 2 3
Test1(P) 2 2
Test1(O) 1 1

生成相应 table 的查询:

db<>fiddle here

CREATE TABLE `products` (
  `id` int(11) NOT NULL,
  `product_name` varchar(150) NOT NULL,
  `qty` int(10) NOT NULL,
  `price` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
;


CREATE TABLE `purchase` (
  `id` int(11) NOT NULL,
  `warehouse_id` int(11) DEFAULT NULL,
  `date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
;

CREATE TABLE `purchase_item` (
  `id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `purchase_id` int(11) NOT NULL,
  `qty` varchar(255) NOT NULL,
  `rate` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
;

CREATE TABLE `order` (
  `id` int(11) NOT NULL,
  `warehouse_id` int(11) DEFAULT NULL,
  `date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
;

CREATE TABLE `order_item` (
  `id` int(11) NOT NULL,
  `product_id` int(11) NOT NULL,
  `order_id` int(11) NOT NULL,
  `qty` varchar(255) NOT NULL,
  `rate` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
;

INSERT INTO `products` (`id`, `product_name`, `qty`, `price`) 
VALUES
(1,  'TEST', 1, '50'),
(2, 'TEST 2', 0, '50'),
(3, 'TEST 3', 0, '50'),
(4,  'TEST 4', 0, '50')
;

INSERT INTO `purchase` (`id`, `warehouse_id`, `date`) VALUES
(1, 1, '2022-03-02');

INSERT INTO `purchase_item` (`id`, `product_id`, `purchase_id`, `qty`, `rate`) VALUES
(1, 1, 1, '2', '50');


INSERT INTO `order` (`id`, `warehouse_id`, `date`) VALUES
(1, 1, '2022-03-02');

INSERT INTO `order_item` (`id`, `product_id`, `order_id`, `qty`, `rate`) VALUES
(1, 1, 1, '2', '50');

这是一个returns您要查找的格式的查询。
我已将其更新为使用 datewarehouse_id 作为 WHERE 子句中的参数。

with stock_movements as
(
select 
  'P' PA,
  pu.warehouse_id ,
  pu.`date` mvtdate,
  p.id,
  concat(p.product_name,'(P)') Pname,
  pi.qty pqty,
  0 as oqty,
  sum(coalesce(pi.qty,0)) Total
from products p
left join purchase_item pi
on p.id = pi.product_id
left join purchase pu
on pu.id = purchase_id
group by 
  pu.warehouse_id,
  p.id,
  p.product_name,
  pu.`date`,
  pi.qty
union all
select
  'A',
  ord.`warehouse_id`,
  ord.`date` mvtDate,
  p.id,
  concat(p.product_name,'(O)'),
  0,
  sum(coalesce(o.qty,0)) oqty,
  sum(coalesce(pi.qty,0))
     -sum(coalesce(o.qty,0))
from products p
left join order_items o
on p.id = o.product_id
left join purchase_item pi
on p.id = pi.product_id
left join `order` ord
on ord.id = order_id
group by
  ord.`warehouse_id`,  
  ord.`date`,
  p.id,
  p.product_name
order by 
  id
)
select
PA,
warehouse_id,
mvtdate "date",
id "ID",
pname "Product Name",
sum(pqty) "Input",
sum(oqty) "Output",
sum(Total) "Total"
from stock_movements
where mvtDate = '2022-03-02'
and warehouse_Id = '1'
group by
id, pname,pa,warehouse_id
order by 
id,pa desc;
PA | warehouse_id | date       | ID | Product Name | Input | Output | Total
:- | -----------: | :--------- | -: | :----------- | ----: | -----: | ----:
P  |            1 | 2022-03-02 |  1 | TEST(P)      |     2 |      0 |     2
A  |            1 | 2022-03-02 |  1 | TEST(O)      |     0 |      1 |     1
P  |            1 | 2022-03-02 |  2 | TEST 2(P)    |     3 |      0 |     3
A  |            1 | 2022-03-02 |  2 | TEST 2(O)    |     0 |      1 |     2

db<>fiddle here

第一个数据库 FIDDLE: *db<>fiddle [此处](https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=a6d7e94af44035d838b2be5bf863a476