2017-09-15 181 views
1

我正試圖計算股票在SQL中。我有3桌產品,購買和銷售。 產品表股票計算總和列

+----+------------------+ 
| id | product_name  | 
+----+------------------+ 
| 1 | apple   | 
|----|------------------| 
| 2 |banana   | 
|----|------------------| 
| 3 |mango    | 
+----+------------------+ 

現在顏色表

+----+------------------+ 
| id | color_name  | 
+----+------------------+ 
| 1 | dark   | 
|----|------------------| 
| 2 | light   | 
+----|------------------+ 

購表人是

+-------+-------------+ 
| id | quantity |color 
+-------+-------------+ 
| 1 | 15  |dark 
+-------+-------------+ 
| 1 | 10  |light 
+-------+-------------+ 
| 2 | 5   |dark 
+-------+-------------+ 
| 3 | 25  |light 
+-------+-------------+ 

,銷售表

+-------+-------------+ 
| id | quantity |color 
+-------+-------------+ 
| 1 | 5   |dark 
+-------+-------------+ 
| 1 | 5   |light 
+-------+-------------+ 
| 2 | 5   |dark 
+-------+-------------+ 
| 3 | 5   |light 
+-------+-------------+ 

採購和銷售表有外鍵ID參考ences產品表的id。現在我想要計算購買和銷售以下形式的現貨供應,即區別顏色的基礎上,也

+----+------------------+-------------+ 
| id | product_name  | quantity |color 
+----+------------------+-------------+ 
| 1 | apple   | 10  |dark 
|----|------------------|-------------| 
| 1 | apple   | 5  |light 
|----|------------------|-------------| 
| 2 |banana   | 0  |dark 
|----|------------------|-------------| 
| 3 |mango    | 20  |light 
+----+------------------+-------------+ 

回答

2

您可以找

SELECT p.id,p.Name,Purchase.purchaseQty-sales.salseQty as totalQty 
FROM Product p 
OUTER APPLY(
     SELECT purchase.id, SUM(purchase.quantity) purchaseQty 
     FROM purchase 
     where purchase.id= p.id 
     GROUP BY purchase.id 
)Purchase 
OUTER APPLY(
     SELECT sales.id, SUM(sales.quantity) salseQty 
     FROM sales 
     where sales.id= p.id 
     GROUP BY sales.id 
)sales 
+0

如果我們有另一個表顏色會是怎樣最好查詢顏色的基礎上計算的股票太像可用數量紅色的蘋果和淺紅色的蘋果 –

+0

我已經改變了這個問題你可以檢查它嗎 –

0
SELECT a.id, a.product_name, 
SUM(b.quantity) - SUM(c.quantity) as 'quantity' 
FROM product a 
LEFT JOIN purchase b 
ON a.id=b.id 
LEFT JOIN sales c 
ON b.id = c.id group by a.id, a.product_name 

(我沒有測試任何東西)

+0

如果某些產品沒有銷售記錄,該怎麼辦?這將是空如果沒有提供如何解決呢 –

+0

有針對的SQL方法,但我不記得(它替換空0) – alexay68

+0

得到它:SELECT ISNULL(c.quantity,0)。語法取決於您使用的數據庫的類型 – alexay68

0

試試這個解決方案與外部適用。

drop table if exists dbo.tProduct; 
drop table if exists dbo.tPurchase; 
drop table if exists dbo.tSale; 

create table dbo.tProduct (
    id int 
    , product_name varchar(100) 
); 

create table dbo.tPurchase (
    id int 
    , quantity int 
); 

create table dbo.tSale (
    id int 
    , quantity int 
); 

insert into dbo.tProduct (id, product_name) 
values (1, 'apple'), (2, 'banana'), (3, 'mango'); 

insert into dbo.tPurchase (id, quantity) 
values (1, 15), (2, 10), (1, 5), (3, 25); 

insert into dbo.tSale (id, quantity) 
values (1, 5), (3, 10), (1, 5), (3, 5), (2, 5); 

select 
    p.id 
    , p.product_name 
    , pur.Quantity - sal.Quantity as Quantity 
from dbo.tProduct p 
    outer apply (
     select 
      sum(tp.quantity) as Quantity 
     from dbo.tPurchase tp 
     where p.id = tp.id 
    ) pur 
    outer apply (
     select 
      sum(tp.quantity) as Quantity 
     from dbo.tSale tp 
     where p.id = tp.id 
    ) sal 
+0

我改變了我的問題你可以檢查它嗎 –