2014-02-05 42 views
1

我在mysql中查看(vwbalance),我使用它來檢查商店中產品的當前庫存餘額,它在一個產品上工作得很好。 下面是兩個表trn_inventory我們存儲所有庫存交易(產品進來和產品走出去)和mst_product我們存儲產品的細節視圖在mysql視圖中計算每個產品的運行餘額

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犯規存儲餘額,下面是表定義

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;

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" 

我想讓它顯示每個產品的平衡:

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; 

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

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在那裏你可以得到這個模式有樣本數據,您可以測試查詢

+2

你選擇和a.'tdate'加入,但按a.'date'? – LauriK

+0

我已更正了錯字 – indago

+0

糾正後結果是否改變? – LauriK

回答

3

你沒有匹配CREATE VIEW兩個表的產品編號。 所以,你需要設置你的條件如下。

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

見工作FIDDLE

+2

非常感謝!它像一個魅力工作! – indago

+1

歡迎您:) –