2014-09-25 114 views
1

我想得到這樣一個表其餘項目:獲取在商店

itemid | remaining_quantity 
-------|--------- 
1  | 100 
-------|--------- 
3  | 150 
-------|--------- 
4  | 300 

顯着表:

  • Purshase(ID,爲itemid,數量,isReceived)//由我們收取?

  • 銷售(id,itemid,數量,isReceived)//是否被客戶接收?

  • 項目(ID,init_quantity)

  • LostItem(ID,商品ID,數量)//這是用來解決實際數量,如果它實際上是增加或減少

,你可以看,我們應該只考慮那些銷售和追求isReceived = True。在LostItem中,數量可能爲負數,表示實際數量少於計算數量。它也可以是積極的,意味着實際的計算更多。

(即我想Purshase - 售+ LostItem +項目)

這裏是我迄今所做的:(在沒有行結果)

select i.itemid, 
     isnull(sum(p.quantity), 0) - isnull(sum(s.quantity), 0) 
     + isnull(sum(t.quantity), 0) + isnull(sum(i.itemInitQuantity), 0) 
    as remaining_quantity 
from [Purshase] p, [Sale] s, [LostItem] t, [Item] i 
where p.pitem = i.itemid and s.saleitem = i.itemid and t.item = i.itemid 
     and p.isReceived = 1 and s.isReceived = 1 
group by i.itemid 
+0

像http://stackoverflow.com/questions/18011962/sqlite-calculate-remaining-item-quantities – 2014-09-25 05:19:10

+0

是你的剩餘量公式是正確的?據我所知,它應該像init_quantity - 購買 - 總銷售 - 完全丟失。 – Mukund 2014-09-25 05:24:08

+0

@Mukund是的,你是對的 – 2014-09-25 05:32:15

回答

0

找到它! (Alhamdu lellah)

SELECT itemid, init, sales_all, sales_mostlma, purchases_all, 
purchases_mstlma, taswiyah, sales_mardood, purchases_mardood, 
sales_request, pruchases_request,init - sales_mostlma + purchases_mstlma + 
taswiyah + sales_mardood - purchases_mardood AS stock, init - sales_all + 
purchases_all + taswiyah + sales_mardood - purchases_mardood AS available, 
init - sales_all + purchases_all + taswiyah + sales_mardood - 
purchases_mardood - sales_request + pruchases_request 
AS available_with_request 
FROM (SELECT itemid,itemInitQuantity AS init, 
(SELECT ISNULL(SUM(salequantity), 0) AS cnt FROM dbo.Sale AS s 
WHERE (saleitem = i.itemid) AND (tasleemTime IS NOT NULL)) AS sales_mostlma, 
(SELECT ISNULL(SUM(salequantity), 0) AS cnt FROM dbo.Sale AS s 
WHERE (saleitem = i.itemid)) AS sales_all, 
(SELECT ISNULL(SUM(pcquantity), 0) AS cnt FROM dbo.Purshase AS p 
WHERE (pitem = i.itemid) AND (tasleemTime IS NOT NULL)) AS purchases_mstlma, 
(SELECT ISNULL(SUM(pcquantity), 0) AS cnt FROM dbo.Purshase AS p 
WHERE (pitem = i.itemid)) AS purchases_all, 
(SELECT ISNULL(SUM(quantity), 0) AS cnt FROM dbo.TaswiyahGardiyah AS t 
WHERE (item = i.itemid)) AS taswiyah, 
(SELECT ISNULL(SUM(sm.smquantity), 0) AS Expr1 FROM dbo.SaleMardood AS sm 
INNER JOIN dbo.Sale AS ss ON sm.smsale = ss.saleid 
WHERE (ss.saleitem = i.itemid)) AS sales_mardood, 
(SELECT  ISNULL(SUM(pm.purmquantity), 0) AS Expr1 
FROM dbo.PurshaseMardood AS pm 
INNER JOIN dbo.Purshase AS pur ON pm.purmpurshase = pur.pid 
WHERE (pur.pitem = i.itemid)) AS purchases_mardood, 
(SELECT ISNULL(SUM(prquantity), 0) AS Expr1 FROM dbo.PurshaseRequest AS pr 
WHERE (pritem = i.itemid)) AS pruchases_request, 
(SELECT ISNULL(SUM(scquantity), 0) AS Expr1 FROM dbo.SaleRequest AS sr 
WHERE (scitem = i.itemid)) AS sales_request 
FROM dbo.Item AS i) AS myTable