提问人:mcode 提问时间:9/3/2023 最后编辑:FanoFNmcode 更新时间:9/11/2023 访问量:75
MySQL 查询的期初余额返回不正确
Incorrect Opening Balance return for MySQL query
问:
我有一个如下视图来过滤接收和发出的项目,如A4,A3,.......在从开始日期到结束日期的选定日期范围内。
收货用“购买”表示,发行用“order_status”下的表格表示为“发行”。如此示例所示,建议仅在日期范围 2023-08-01 到 2023-08-31 内存在问题。通过以下代码在表单输出中收到数量
(CASE store_update_stock.order_status
WHEN "ob" AND "purchase" AND billed_date <= "$start"
THEN store_update_stock_details.qty
ELSE 0
END) p2,
所需输出
我想获得截至 2023 年 7 月 31 日的期末余额,以及截至 2023 年 8 月 1 日的期初余额,例如 A4 的 255。然后,可以从该期初余额中扣除问题。
错误
但是,当问题正确输出时,筛选器会输出“0”作为开始日期。
我的模型如下:
public function issueDetailReport($id,$start,$end){
$this->db->select('*,
(CASE
WHEN store_update_stock.order_status = "issue" THEN store_branch.branch_name
ELSE tbl_supplier.supplier_name
END) supplier_officer_name,
(CASE
WHEN store_update_stock.order_status = "issue" THEN store_update_stock.request_no
ELSE store_update_stock.bill_no
END) number,
(CASE store_update_stock.order_status
WHEN "issue" AND store_update_stock.billed_date <= "$start"
THEN store_update_stock_details.qty * (-1)
ELSE store_update_stock_details.qty * (-1)
END) quantity,
(CASE store_update_stock.order_status
WHEN "purchase" AND billed_date <= "$start"
THEN store_update_stock_details.qty
ELSE 0
END) p2,
(CASE store_update_stock.order_status
WHEN "purchase" THEN store_update_stock_details.qty AND store_update_stock.billed_date <= "$start"
WHEN "issue" THEN store_update_stock_details.qty AND store_update_stock.billed_date <= "$start"
END) balance
');
$this->db->from('store_update_stock');
$this->db->join('store_update_stock_details','store_update_stock.update_stock_id=store_update_stock_details.update_stock_id', 'inner');
$this->db->join('store_branch','store_update_stock.branch_id=store_branch.branch_id', 'left' );
$this->db->join('tbl_supplier','store_update_stock.supplier=tbl_supplier.supplier_id', 'left');
$this->db->join('store_item','store_update_stock_details.item=store_item.item_id', 'left');
$this->db->where("store_update_stock.status='1' and store_item.item_id=$id");
$this->db->where("store_update_stock_details.qty <> 0");
if($start!=NULL && $end!=NULL)
$this->db->where("store_update_stock.billed_date BETWEEN '$start' AND '$end'");
$this->db->order_by('store_update_stock.billed_date','ASC');
$q=$this->db->get();
if($q->num_rows()>0){
return $q->result();
}
return false;
}
出了什么问题。谁能帮忙?
答:
0赞
FanoFN
9/4/2023
#1
在MySQL v8中,您可以使用像AND这样的窗口函数来实现如下结果:SUM() OVER ()
LAG()
ORDER_status | billed_date | Previous_balance | 数量 | Qty_balance |
---|---|---|---|---|
收到 | 2023-07-15 | 零 | 100 | 100 |
发出 | 2023-07-16 | 100 | 5 | 95 |
发出 | 2023-07-17 | 95 | 10 | 85 |
发出 | 2023-07-18 | 85 | 15 | 70 |
发出 | 2023-07-19 | 70 | 11 | 59 |
发出 | 2023-07-21 | 59 | 3 | 56 |
发出 | 2023-07-23 | 56 | 9 | 47 |
收到 | 2023-07-25 | 47 | 100 | 147 |
发出 | 2023-07-26 | 147 | 20 | 127 |
发出 | 2023-07-27 | 127 | 19 | 108 |
发出 | 2023-07-28 | 108 | 8 | 100 |
发出 | 2023-07-30 | 100 | 7 | 93 |
发出 | 2023-07-31 | 93 | 9 | 84 |
发出 | 2023-08-01 | 84 | 5 | 79 |
发出 | 2023-08-02 | 79 | 9 | 70 |
发出 | 2023-08-03 | 70 | 6 | 64 |
发出 | 2023-08-04 | 64 | 7 | 57 |
发出 | 2023-08-05 | 57 | 11 | 46 |
发出 | 2023-08-06 | 46 | 21 | 25 |
这是查询:
WITH cte AS
(SELECT order_status,
billed_date,
qty,
SUM(IF(order_status='received',qty,0)-IF(order_status='issued',qty,0))
OVER (ORDER BY billed_date) AS Qty_balance
FROM store_update_stock
ORDER BY billed_date)
SELECT order_status,
billed_date,
LAG(Qty_balance) OVER (ORDER BY billed_date) Previous_balance,
qty,
Qty_balance
FROM cte;
..或者,如果您更熟悉派生表,只需将查询上方放在括号中,然后为其分配别名即可。SELECT ..
SELECT order_status,
billed_date,
LAG(Qty_balance) OVER (ORDER BY billed_date) Previous_balance,
qty,
Qty_balance
FROM (SELECT order_status,
billed_date,
qty,
SUM(IF(order_status='received',qty,0)-IF(order_status='issued',qty,0))
OVER (ORDER BY billed_date) AS Qty_balance
FROM store_update_stock
ORDER BY billed_date) a;
这里有一把小提琴:https://dbfiddle.uk/-vWlsi8w
评论