民间, 我想在这里做一些事情,但它没有成功。基本上我想列出未清项目,但是如果已经发货了一些数量。我想从公开项目中扣除它。
打开列=订购数量 - 已开帐单的数量,但如果原始订单的某些部分已经开帐单,那么也需要扣除。
SELECT
so.so_create_on AS 'Sales_Order_Created_On',
so.sales_order AS 'Sales_Order',
li.so_item 'Sales_Order_Item',
so.site_id_name_1 'Site_ID_Name_1',
li.order_quantity AS 'Order_Quantity',
li.so_net_price AS 'Sales_Order_Net_Price',
(li.order_quantity * li.so_net_price) AS 'Sales_Order Net Value',
bi.bill_item AS 'Billed_Item',
bi.bill_qty AS 'Billed_Quantity',
bi.bill_doc_date AS 'Billed_Document_Date',
(bi.bill_qty * li.so_net_price) AS 'Billed Net Value'
FROM sap_sales_order so
LEFT JOIN sap_so_line_item li ON so.sales_order = li.sales_order
LEFT JOIN sap_billing bi ON so.sales_order = bi.sales_order AND (li.so_item = bi.bill_item or bi.bill_item = 0)
+------------------------+-------------+------------------+----------------+----------------+-----------------------+-----------------------+-------------+-----------------+----------------------+------------------+
| Sales_Order_Created_On | Sales_Order | Sales_Order_Item | Site_ID_Name_1 | Order_Quantity | Sales_Order_Net_Price | Sales_Order Net Value | Billed_Item | Billed_Quantity | Billed_Document_Date | Billed Net Value |
+------------------------+-------------+------------------+----------------+----------------+-----------------------+-----------------------+-------------+-----------------+----------------------+------------------+
| 2015-09-23 | 1209549436 | 10 | DEF | 2500 | 1.00 | 2500.00 | 10 | 845 | 2015-11-23 | 845.00 |
| 2015-09-23 | 1209549436 | 20 | DEF | 8800 | 1.00 | 8800.00 | 20 | 8800 | 2015-11-23 | 8800.00 |
| 2015-09-23 | 1209549436 | 10 | DEF | 2500 | 1.00 | 2500.00 | 10 | 1050 | 2015-12-14 | 1050.00 |
| 2015-09-23 | 1209549436 | 10 | DEF | 2500 | 1.00 | 2500.00 | 10 | 500 | 2015-12-21 | 500.00 |
| 2015-09-23 | 1209549436 | 10 | DEF | 2500 | 1.00 | 2500.00 | 10 | 105 | 2015-12-21 | 105.00 |
| 2016-04-28 | 1230503910 | 10 | DEF | 9500 | 1.00 | 9500.00 | 10 | 1000 | 2016-07-18 | 1000.00 |
| 2016-04-28 | 1230503910 | 10 | DEF | 9500 | 1.00 | 9500.00 | 10 | 3775 | 2016-07-29 | 3775.00 |
| 2016-04-28 | 1230503910 | 10 | DEF | 9500 | 1.00 | 9500.00 | 10 | 2500 | 2016-07-29 | 2500.00 |
| 2016-04-28 | 1230503910 | 10 | DEF | 9500 | 1.00 | 9500.00 | 10 | 2225 | 2016-10-19 | 2225.00 |
| 2017-02-03 | 1270633456 | 10 | ABC | 1 | 50000.00 | 50000.00 | 10 | 1 | 2017-02-08 | 50000.00 |
| 2017-02-03 | 1270633456 | 20 | ABC | 1 | 145000.00 | 145000.00 | 20 | 1 | 2017-02-08 | 145000.00 |
| 2016-07-28 | 111831121 | 10 | GHI | 20 | 0.75 | 15.00 | 10 | 20 | 2016-07-29 | 15.00 |
| 2016-07-28 | 0111831121 | 20 | GHI | 50 | 0.75 | 37.50 | 20 | 50 | 2016-07-29 | 37.50 |
| 2017-02-03 | 1270633456 | 10 | ABC | 1 | 50000.00 | 50000.00 | 0 | 0 | NULL | 0.00 |
| 2017-02-03 | 1270633456 | 20 | ABC | 1 | 145000.00 | 145000.00 | 0 | 0 | NULL | 0.00 |
| 2017-02-03 | 1270633456 | 30 | ABC | 15 | 2200.00 | 33000.00 | 0 | 0 | NULL | 0.00 |
| 2017-02-03 | 1270633456 | 40 | ABC | 25000 | 1.00 | 25000.00 | 0 | 0 | NULL | 0.00 |
| 2017-02-28 | 1275536210 | 10 | DEF | 1000 | 1.00 | 1000.00 | 0 | 0 | NULL | 0.00 |
| 2017-02-28 | 1275536210 | 20 | DEF | 1450 | 1.00 | 1450.00 | 0 | 0 | NULL | 0.00 |
| 2017-02-28 | 1275536210 | 30 | DEF | 145 | 1.00 | 145.00 | 0 | 0 | NULL | 0.00 |
| 2017-02-28 | 1275536210 | 40 | DEF | 880 | 1.00 | 880.00 | 0 | 0 | NULL | 0.00 |
+------------------------+-------------+------------------+----------------+----------------+-----------------------+-----------------------+-------------+-----------------+----------------------+------------------+
+------+------------------------+-------------+------------------+----------------+----------------+-----------------------+-----------------------+-------------+-----------------+----------------------+------------------+
| Open | Sales_Order_Created_On | Sales_Order | Sales_Order_Item | Site_ID_Name_1 | Order_Quantity | Sales_Order_Net_Price | Sales_Order Net Value | Billed_Item | Billed_Quantity | Billed_Document_Date | Billed Net Value |
+------+------------------------+-------------+------------------+----------------+----------------+-----------------------+-----------------------+-------------+-----------------+----------------------+------------------+
| 1655 | 2015-09-23 | 1209549436 | 10 | DEF | 2500 | 1.00 | 2500.00 | 10 | 845 | 2015-11-23 | 845.00 |
| 0 | 2015-09-23 | 1209549436 | 20 | DEF | 8800 | 1.00 | 8800.00 | 20 | 8800 | 2015-11-23 | 8800.00 |
| 1450 | 2015-09-23 | 1209549436 | 10 | DEF | 2500 | 1.00 | 2500.00 | 10 | 1050 | 2015-12-14 | 1050.00 |
| 950 | 2015-09-23 | 1209549436 | 10 | DEF | 2500 | 1.00 | 2500.00 | 10 | 500 | 2015-12-21 | 500.00 |
| 845 | 2015-09-23 | 1209549436 | 10 | DEF | 2500 | 1.00 | 2500.00 | 10 | 105 | 2015-12-21 | 105.00 |
| 8500 | 2016-04-28 | 1230503910 | 10 | DEF | 9500 | 1.00 | 9500.00 | 10 | 1000 | 2016-07-18 | 1000.00 |
| 4725 | 2016-04-28 | 1230503910 | 10 | DEF | 9500 | 1.00 | 9500.00 | 10 | 3775 | 2016-07-29 | 3775.00 |
| 2225 | 2016-04-28 | 1230503910 | 10 | DEF | 9500 | 1.00 | 9500.00 | 10 | 2500 | 2016-07-29 | 2500.00 |
| 0 | 2016-04-28 | 1230503910 | 10 | DEF | 9500 | 1.00 | 9500.00 | 10 | 2225 | 2016-10-19 | 2225.00 |
| 0 | 2017-02-03 | 1270633456 | 10 | ABC | 1 | 50000.00 | 50000.00 | 10 | 1 | 2017-02-08 | 50000.00 |
| 0 | 2017-02-03 | 1270633456 | 20 | ABC | 1 | 145000.00 | 145000.00 | 20 | 1 | 2017-02-08 | 145000.00 |
| 0 | 2016-07-28 | 111831121 | 10 | GHI | 20 | 0.75 | 15.00 | 10 | 20 | 2016-07-29 | 15.00 |
| 0 | 2016-07-28 | 0111831121 | 20 | GHI | 50 | 0.75 | 37.50 | 20 | 50 | 2016-07-29 | 37.50 |
| 1 | 2017-02-03 | 1270633456 | 10 | ABC | 1 | 50000.00 | 50000.00 | 0 | 0 | NULL | 0.00 |
| 1 | 2017-02-03 | 1270633456 | 20 | ABC | 1 | 145000.00 | 145000.00 | 0 | 0 | NULL | 0.00 |
| 15 | 2017-02-03 | 1270633456 | 30 | ABC | 15 | 2200.00 | 33000.00 | 0 | 0 | NULL | 0.00 |
|25000 | 2017-02-03 | 1270633456 | 40 | ABC | 25000 | 1.00 | 25000.00 | 0 | 0 | NULL | 0.00 |
| 1000 | 2017-02-28 | 1275536210 | 10 | DEF | 1000 | 1.00 | 1000.00 | 0 | 0 | NULL | 0.00 |
| 1450 | 2017-02-28 | 1275536210 | 20 | DEF | 1450 | 1.00 | 1450.00 | 0 | 0 | NULL | 0.00 |
| 145 | 2017-02-28 | 1275536210 | 30 | DEF | 145 | 1.00 | 145.00 | 0 | 0 | NULL | 0.00 |
| 880 | 2017-02-28 | 1275536210 | 40 | DEF | 880 | 1.00 | 880.00 | 0 | 0 | NULL | 0.00 |
+------+------------------------+-------------+------------------+----------------+----------------+-----------------------+-----------------------+-------------+-----------------+----------------------+------------------+
--
-- Table structure for table `sap_billing`
--
CREATE TABLE `sap_billing` (
`bill_item` char(15) NOT NULL,
`bill_qty` int(10) UNSIGNED DEFAULT NULL,
`bill_doc_date` date DEFAULT NULL,
`bill_doc` char(15) NOT NULL,
`bill_net_value` decimal(11,2) DEFAULT NULL,
`sales_order` char(15) NOT NULL,
`import_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `sap_billing`
--
INSERT INTO `sap_billing` (`bill_item`, `bill_qty`, `bill_doc_date`, `bill_doc`, `bill_net_value`, `sales_order`, `import_date`) VALUES
('10', 845, '2015-11-23', '3206790137', '845.00', '1209549436', '2017-03-13'),
('20', 8800, '2015-11-23', '3206790137', '8800.00', '1209549436', '2017-03-13'),
('10', 1050, '2015-12-14', '3209056079', '1050.00', '1209549436', '2017-03-13'),
('10', 500, '2015-12-21', '3209763880', '500.00', '1209549436', '2017-03-13'),
('10', 105, '2015-12-21', '3209763885', '105.00', '1209549436', '2017-03-13'),
('10', 1000, '2016-07-18', '3230230297', '1000.00', '1230503910', '2017-03-13'),
('10', 3775, '2016-07-29', '3231648901', '3775.00', '1230503910', '2017-03-13'),
('10', 2500, '2016-07-29', '3231648902', '2500.00', '1230503910', '2017-03-13'),
('10', 2225, '2016-10-19', '3240098774', '2225.00', '1230503910', '2017-03-13'),
('10', 1, '2017-02-08', '3250923886', '50000.00', '1270633456', '2017-03-13'),
('20', 1, '2017-02-08', '3250923886', '145000.00', '1270633456', '2017-03-13'),
('10', 20, '2016-07-29', '5021718928', '15.00', '111831121', '2017-03-13'),
('20', 50, '2016-07-29', '5021718928', '37.50', '0111831121', '2017-03-13'),
('0', 0, NULL, 'NA', '0.00', '1270633456', '2017-03-13'),
('0', 0, NULL, 'NA', '0.00', '1275536210', '2017-03-13');
-- --------------------------------------------------------
--
-- Table structure for table `sap_sales_order`
--
CREATE TABLE `sap_sales_order` (
`sales_order` char(15) NOT NULL,
`order_reason` varchar(150) DEFAULT NULL,
`so_create_on` date DEFAULT NULL,
`sold_to_party` char(15) DEFAULT NULL,
`customer_po` varchar(150) DEFAULT NULL,
`site_id_name_1` varchar(150) DEFAULT NULL,
`import_date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `sap_sales_order`
--
INSERT INTO `sap_sales_order` (`sales_order`, `order_reason`, `so_create_on`, `sold_to_party`, `customer_po`, `site_id_name_1`, `import_date`) VALUES
('0111831121', NULL, '2016-07-28', 'A1', NULL, 'GHI', '2017-03-13'),
('111831121', NULL, '2016-07-28', 'A1', NULL, 'GHI', '2017-03-13'),
('1209549436', NULL, '2015-09-23', '45342', 'PPQQRR', 'DEF', '2017-03-13'),
('1230503910', NULL, '2016-04-28', '45342', 'OMI1212', 'DEF', '2017-03-13'),
('1270633456', NULL, '2017-02-03', 'AV', '123456', 'ABC', '2017-03-13'),
('1275536210', NULL, '2017-02-28', '45342', '1A', 'DEF', '2017-03-13');
-- --------------------------------------------------------
--
-- Table structure for table `sap_so_line_item`
--
CREATE TABLE `sap_so_line_item` (
`so_item` int(10) UNSIGNED NOT NULL,
`material` varchar(45) CHARACTER SET latin1 DEFAULT NULL,
`description` varchar(150) CHARACTER SET latin1 DEFAULT NULL,
`order_quantity` int(10) UNSIGNED DEFAULT NULL,
`so_net_price` decimal(11,2) DEFAULT NULL,
`so_net_value` decimal(11,2) DEFAULT NULL,
`sales_order` char(15) CHARACTER SET latin1 NOT NULL,
`import_date` date NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `sap_so_line_item`
--
INSERT INTO `sap_so_line_item` (`so_item`, `material`, `description`, `order_quantity`, `so_net_price`, `so_net_value`, `sales_order`, `import_date`) VALUES
(10, 'M1', 'K1', 20, '0.75', '15.00', '111831121', '2017-03-13'),
(10, 'M4', 'K4', 2500, '1.00', '2500.00', '1209549436', '2017-03-13'),
(10, 'M4', 'K4', 9500, '1.00', '9500.00', '1230503910', '2017-03-13'),
(10, 'M3', 'K3', 1, '50000.00', '50000.00', '1270633456', '2017-03-13'),
(10, 'M4', 'K4', 1000, '1.00', '1000.00', '1275536210', '2017-03-13'),
(20, 'M1', 'K1', 50, '0.75', '37.50', '0111831121', '2017-03-13'),
(20, 'M2', 'K2', 8800, '1.00', '8800.00', '1209549436', '2017-03-13'),
(20, 'M5', 'K5', 1, '145000.00', '145000.00', '1270633456', '2017-03-13'),
(20, 'M4', 'K4', 1450, '1.00', '1450.00', '1275536210', '2017-03-13'),
(30, 'M6', 'K6', 15, '2200.00', '33000.00', '1270633456', '2017-03-13'),
(30, 'M4', 'K4', 145, '1.00', '145.00', '1275536210', '2017-03-13'),
(40, 'M7', 'K7', 25000, '1.00', '25000.00', '1270633456', '2017-03-13'),
(40, 'M4', 'K4', 880, '1.00', '880.00', '1275536210', '2017-03-13');
-- --------------------------------------------------------
答案 0 :(得分:2)
执行此操作的方法是使用sql变量,正如McNets的回答所示。
SET @last_sales_order := 0, @next_open = 0;
SELECT
sales_order,
so_create_on,
order_quantity,
bill_item,
bill_qty,
bill_doc,
bill_doc_date,
`Open`
FROM
(SELECT
sa.sales_order,
sa.so_create_on,
sa.order_quantity,
sh.bill_item,
sh.bill_qty,
sh.bill_doc,
sh.bill_doc_date,
@next_open:=IF(@last_sales_order != sh.sales_order, sa.order_quantity - sh.bill_qty, @next_open - sh.bill_qty) AS 'Open',
@last_sales_order:=sh.sales_order
FROM
sales_order sa
LEFT JOIN shipped_order sh ON sa.sales_order = sh.sales_order) AS result
外部查询只是为了美化生成的列集,该列具有一个额外的列,用于检测行之间的sales_order
更改,以便正确计算Open
列。
答案 1 :(得分:1)
您正在寻找累积和解决方案:
在此处查看:http://rextester.com/SEZS4911
select if (@last_order <> sales_order, @acm_order := order_quantity - bill_qty, @acm_order := @acm_order - bill_qty) as Open,
sales_order,
so_create_on,
order_quantity,
bill_item,
bill_qty,
bill_doc,
bill_doc_date,
@last_order := sales_order
from
(select @last_order := 0, @acm_order := 0) x,
(SELECT sa.sales_order,
sa.so_create_on,
sa.order_quantity,
sh.bill_item,
sh.bill_qty,
sh.bill_doc,
sh.bill_doc_date
FROM sales_order sa
LEFT JOIN shipped_order sh ON sa.sales_order = sh.sales_order
order by sa.sales_order, sh.bill_doc_date) y
;