久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在mysql視圖中計算每個產品的運行余額

Calculate running balance per product in mysql view(在mysql視圖中計算每個產品的運行余額)
本文介紹了在mysql視圖中計算每個產品的運行余額的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

限時送ChatGPT賬號..

我在 mysql 中有一個視圖 (vwbalance),我用它來檢查我們產品在商店中的當前庫存余額,它對一種產品運行良好.這是視圖

I have a view (vwbalance) in mysql that i use to check the current inventory balance of our product in the store,It has been working very fine with one product. Here is the view

CREATE VIEW vwbalance AS
SELECT
  a.`id`      AS `Trans No`,
  a.`tdate` AS Siku,
  a.`section` AS `Section`,
  `g`.`p_name` AS `Product`,
  a.`cr`      AS `In`,
  a.`dr`      AS `Out`,
  SUM((o.`cr` - o.`dr`)) AS `balance`,
  a.`status`  AS `status`
FROM ((`trn_inventory` a
    LEFT JOIN `mst_product` `g`
      ON ((`g`.`p_id` = a.`p_id`)))
   JOIN `trn_inventory` o
     ON (((a.`tdate` > o.`tdate`)
           OR ((a.`tdate` = o.`tdate`)
               AND (a.`id` >= o.`id`)))))
WHERE (o.`status` = 'APPROVED')
GROUP BY a.`tdate` DESC,a.`id` DESC;

上述視圖從兩個表 trn_inventory 中獲取數據,我們在其中存儲所有庫存交易(產品進貨和產品出貨)和 mst_product 表,我們存儲產品詳細信息.我們創建這個視圖的主要原因基本上是為了顯示運行余額,因為表 trn_inventory 不存儲余額,下面是表定義

The above view gets data from two tables trn_inventory where we store all inventory transactions(products coming in and products going out) and mst_product where we store the product details. Our main reason in creating this view is basically to show the running balance because the table trn_inventory doesnt store the balance, below is the table definition

CREATE TABLE trn_inventory (
  id INT(25) NOT NULL AUTO_INCREMENT,
  tdate DATE NOT NULL,
   p_id INT(25) NOT NULL,
   dr INT(5) DEFAULT '0' COMMENT 'OUT',
  cr INT(5) DEFAULT '0' COMMENT 'IN',
  cost DOUBLE(13,2) NOT NULL DEFAULT '0.00',
  section VARCHAR(95) DEFAULT NULL,
  ref VARCHAR(95) DEFAULT NULL,
  trans_user VARCHAR(35) NOT NULL,
  `status` ENUM('PENDING','APPROVED','DISPATCHED','VOID') NOT NULL DEFAULT 'PENDING',
  approvedby VARCHAR(35) DEFAULT NULL,
  dispatchedby VARCHAR(35) DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=INNODB DEFAULT CHARSET=latin1;

這是我運行 SELECT * FROM vwbalance; 時的輸出:

Here is the output when I run SELECT * FROM vwbalance;:

Trans  Siku      Section   Product         In   Out  Bal  Status  
-------------------------------------------------------------------
8   2014-02-05  "Store"   "Treated SEEDS"   0   10   68  "APPROVED"
7   2014-02-05  "Store"   "Treated SEEDS"  50    0   78  "APPROVED"
5   2014-02-04  "Store"   "Dry Seeds"      40    0   28  "APPROVED"
3   2014-01-16  "Store"   "Dry Seeds"       0    2  -12  "APPROVED"
4   2014-01-15  "Store"   "Dry Seeds"       0   15  -10  "APPROVED"
2   2014-01-15  "Store"   "Dry Seeds"      10    0    5      "VOID"
1   2014-01-15  "store"   "Dry Seeds"      12    0    5  "APPROVED"
6   2014-01-14  "Store"   "Dry Seeds"       0    7   -7  "APPROVED"

我希望它顯示每個產品的余額:

I want it to show the balance per product:

Trans  Siku      Section   Product         In   Out  Bal  Status  
-------------------------------------------------------------------
8   2014-02-05  "Store"   "Treated SEEDS"   0   10   40  "APPROVED"
7   2014-02-05  "Store"   "Treated SEEDS"  50    0   50  "APPROVED"
5   2014-02-04  "Store"   "Dry Seeds"      40    0   28  "APPROVED"
3   2014-01-16  "Store"   "Dry Seeds"       0    2  -12  "APPROVED"
4   2014-01-15  "Store"   "Dry Seeds"       0   15  -10  "APPROVED"
2   2014-01-15  "Store"   "Dry Seeds"      10    0    5      "VOID"
1   2014-01-15  "store"   "Dry Seeds"      12    0    5  "APPROVED"
6   2014-01-14  "Store"   "Dry Seeds"       0    7   -7  "APPROVED"

我修改了分組,

...
...
 WHERE (o.`status` = 'APPROVED')
    GROUP BY a.`tdate` DESC,a.`id` DESC,o.p_id;

但它為下面的第二個產品返回兩行是輸出

but it was returning two rows for the second product below is the output

Trans  Siku      Section   Product         In   Out  Bal  Status  
-------------------------------------------------------------------
 8  2014-02-05  "Store"   "Treated SEEDS"   0   10   28  "APPROVED"
 8  2014-02-05  "Store"   "Treated SEEDS"   0   10   40  "APPROVED"
 7  2014-02-05  "Store"   "Treated SEEDS"  50    0   28  "APPROVED"
 7  2014-02-05  "Store"   "Treated SEEDS"  50    0   50  "APPROVED"
 5  2014-02-04  "Store"   "Dry Seeds"      40    0   28  "APPROVED"
 3  2014-01-16  "Store"   "Dry Seeds"       0    2  -12  "APPROVED"
 4  2014-01-15  "Store"   "Dry Seeds"       0   15  -10  "APPROVED"
 2  2014-01-15  "Store"   "Dry Seeds"      10    0    5      "VOID"
 1  2014-01-15  "store"   "Dry Seeds"      12    0    5  "APPROVED"
 6  2014-01-14  "Store"   "Dry Seeds"       0    7   -7  "APPROVED"

我哪里出錯了?

我創建了一個 SQLFiddle,您可以在其中使用示例數據獲取此架構可以測試查詢

I have created an SQLFiddle where you can get this schema with sample data where you can test the query

推薦答案

您在創建視圖中沒有匹配兩個表的產品 ID.所以,你需要設置你的條件如下.

You did not match product id of both tables in create view. So, you need to set your condition as below.

 WHERE (o.status = 'APPROVED' and o.p_id = a.p_id)
GROUP BY a.`tdate` DESC,a.`id` DESC,o.p_id;

參見FIDDLE

這篇關于在mysql視圖中計算每個產品的運行余額的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What SQL Server Datatype Should I Use To Store A Byte[](我應該使用什么 SQL Server 數據類型來存儲字節 [])
Interpreting type codes in sys.objects in SQL Server(解釋 SQL Server 中 sys.objects 中的類型代碼)
Typeorm Does not return all data(Typeorm 不返回所有數據)
Typeorm .loadRelationCountAndMap returns zeros(Typeorm .loadRelationCountAndMap 返回零)
How to convert #39;2016-07-01 01:12:22 PM#39; to #39;2016-07-01 13:12:22#39; hour format?(如何將“2016-07-01 01:12:22 PM轉換為“2016-07-01 13:12:22小時格式?)
MS SQL: Should ISDATE() Return quot;1quot; when Cannot Cast as Date?(MS SQL:ISDATE() 是否應該返回“1?什么時候不能投射為日期?)
主站蜘蛛池模板: 精品美女久久久 | 精品一区二区久久久久久久网精 | 国产激情在线看 | 精品久久久久久久久久久 | 欧美一级黄色免费 | 欧美日韩国产精品激情在线播放 | 狠狠干天天干 | 日日干日日操 | 91网站视频在线观看 | 免费在线播放黄色 | 亚洲免费在线 | cao在线| 欧美在线天堂 | 国产九九精品 | 蜜桃视频一区二区三区 | 国产美女网站 | 欧州一区二区三区 | 在线免费观看毛片 | 国产精品日韩欧美一区二区 | 欧美综合在线视频 | 欧美黄色大片在线观看 | 亚洲精品1区 | 成人免费区一区二区三区 | 久久久久免费观看 | 婷婷成人在线 | 午夜在线观看视频 | 精品一区二区三区四区 | 久久精品欧美一区二区三区不卡 | 久久精品av麻豆的观看方式 | 色视频www在线播放国产人成 | 性欧美精品一区二区三区在线播放 | 久综合| 欧美毛片免费观看 | 四虎在线观看 | 毛片视频网站 | 99pao成人国产永久免费视频 | 毛片免费观看视频 | 成年人的视频免费观看 | 亚洲综合大片69999 | 亚洲日本乱码在线观看 | 91精品国产91久久久久久密臀 |