2013-02-18 55 views
1

由於我的問題有點特別,我還沒有找到答案,並經過很長時間的搜索後,所以在這裏:我有兩個表:In_Stock和Out_Stock。我用下面的選擇:
從SQL中的兩個不同表中刪除

IN_STOCK:

select 
INs.CatID as CategoryID, 
INs.SubCatID as SubcategoryID, Sum(INs.Quantity) as QuantityIN 
from IN_Stock INs 
group by INs.CatID, INs.SubCatID 
╔════════════╦═══════════════╦════════════╗ 
║ CategoryID ║ SubcategoryID ║ QuantityIN ║ 
╠════════════╬═══════════════╬════════════╣ 
║   2 ║    9 ║ 0   ║ 
║   1 ║   16 ║ 8   ║ 
║   1 ║   27 ║ 5   ║ 
║   1 ║   30 ║ 160  ║ 
║   1 ║   31 ║ 6   ║ 
║   1 ║   39 ║ 35   ║ 
║   1 ║   40 ║ 7   ║ 
║   2 ║   44 ║ 13   ║ 
║   2 ║   54 ║ 6   ║ 
║   2 ║   70 ║ 5   ║ 
║   3 ║   87 ║ 3,5  ║ 
╚════════════╩═══════════════╩════════════╝ 

OUT_Stock:

select 
OUTs.CatID as CategoryID, 
OUTs.SubCatID as SubcategoryID, 
Sum(OUTs.Quantity) as QuantityOUT 
from OUT_Stock OUTs 
group by OUTs.CatID, OUTs.SubCatID 
╔════════════╦═══════════════╦═════════════╗ 
║ CategoryID ║ SubcategoryID ║ QuantityOUT ║ 
╠════════════╬═══════════════╬═════════════╣ 
║   1 ║   30 ║   30 ║ 
║   1 ║   39 ║   15 ║ 
╚════════════╩═══════════════╩═════════════╝ 

我得到的是這個表波紋管(這顯然不是正確的)。

select 
INs.CatID as CategoryID, 
INs.SubCatID as SubcategoryID, 
Sum(INs.Quantity) as QuantityIN, 
SUM(OUTs.Quantity) as QuantityOUT, 
SUM(INs.Quantity)- SUM(OUTs.Quantity) as RemainingQuantity 
from IN_Stock INs 
left join OUT_Stock OUTs on INs.CatID=OUTs.CatID and INs.SubCatid=OUTs.SubCatid 
group by INs.catid, INs.subcatid 

╔════════════╦═══════════════╦═════════════╦════════════╦═══════════════════╗ 
║ CategoryID ║ SubcategoryID ║ QuantityIN ║ QuantityOUT║ RemainingQuantity ║ 
╠════════════╬═══════════════╬═════════════╬════════════╬═══════════════════╣ 
║   2 ║    9 ║ 0   ║   ║     ║ 
║   1 ║   16 ║ 8   ║   ║     ║ 
║   1 ║   27 ║ 5   ║   ║     ║ 
║   1 ║   30 ║ 320   ║  150 ║    170 ║ 
║   1 ║   31 ║ 6   ║   ║     ║ 
║   1 ║   39 ║ 35   ║   30 ║     5 ║ 
║   1 ║   40 ║ 7   ║   ║     ║ 
║   2 ║   44 ║ 13   ║   ║     ║ 
║   2 ║   54 ║ 6   ║   ║     ║ 
║   2 ║   70 ║ 5   ║   ║     ║ 
║   3 ║   87 ║ 3,5   ║   ║     ║ 
╚════════════╩═══════════════╩═════════════╩════════════╩═══════════════════╝ 

我想要的是使SQL返回類似表波紋管一個選擇......我想知道是否和如何我可以在RemaningStock collumn看到:其中SubCategoryID = 30和其中SubCategoryID = 39。

╔════════════╦═══════════════╦════════════╦════════════╦═══════════════════╗ 
║ CategoryID ║ SubcategoryID ║ QuantityIN ║ QuantityIN ║ RemainingQuantity ║ 
╠════════════╬═══════════════╬════════════╬════════════╬═══════════════════╣ 
║   2 ║    9 ║ 0   ║   ║     ║ 
║   1 ║   16 ║ 8   ║   ║     ║ 
║   1 ║   27 ║ 5   ║   ║     ║ 
║   1 ║   30 ║ 160  ║   30 ║    130 ║ 
║   1 ║   31 ║ 6   ║   ║     ║ 
║   1 ║   39 ║ 35   ║   15 ║    20 ║ 
║   1 ║   40 ║ 7   ║   ║     ║ 
║   2 ║   44 ║ 13   ║   ║     ║ 
║   2 ║   54 ║ 6   ║   ║     ║ 
║   2 ║   70 ║ 5   ║   ║     ║ 
║   3 ║   87 ║ 3,5  ║   ║     ║ 
╚════════════╩═══════════════╩════════════╩════════════╩═══════════════════╝ 

兩個表有一個或多個記錄某一類別或子類別
任何幫助深表感謝。非常感謝!
SQL或Access VBA代碼對我來說都很好。
PS:由於這是我的第一篇文章,請「溫柔」。

+0

+1該表的格式! – Brad 2013-02-18 15:16:30

回答

1

主要問題是您的最終查詢會在初始數據集中的單個行上進行連接,然後纔會執行聚合,而您希望對中間總和執行連接。

假設本次測試數據,例如:

CREATE TABLE table_in (
    id INTEGER, 
    value INTEGER 
); 

CREATE TABLE table_out (
    id INTEGER, 
    value INTEGER 
); 

INSERT INTO table_in(id, value) VALUES 
    (1, 120), 
    (1, 10); 

INSERT INTO table_out(id, value) VALUES 
    (1, 30); 

你在你的最後一個查詢寫LEFT JOIN方式:

SELECT t1.value AS val1, t2.value AS val2 
    FROM table_in t1 LEFT JOIN table_out t2 ON t1.id=t2.id; 

會產生這些行,聚集前:

ID  VAL1  VAL2 
1  120   30 
1  10   30 

這裏,總和會給出這個:

ID  SUM(VAL1) SUM(VAL2) 
1  130   60 

這會發生在用於連接的條件不止一行的情況下。

您需要在之後執行加入,因爲您想要將所有輸入的總和與所有輸出的總和進行比較。

這可以使用subselect語句或CTEs完成。

例如:

WITH sum_in AS (
    SELECT id, SUM(value) AS all_in 
    FROM table_in 
    GROUP BY id 
), sum_out AS (
    SELECT id, SUM(value) AS all_out 
    FROM table_out 
    GROUP BY id 
) 
SELECT t1.id, all_in, all_out, all_in - all_out 
    FROM sum_in t1 LEFT JOIN sum_out t2 ON t1.id=t2.id 
+0

'WITH SUM_IN AS( SELECT INs.Catid,INs.Subcatid,SUM(INs.Quantity)AS all_in FROM IN_STOCK諸IN GROUP BY INs.catid,INs.subcatid ),SUM_OUT AS( SELECT OUTs.Catid, OUTs.Subcatid,SUM(OUTs.Quantity)AS all_out FROM OUT_Stock OUTs GROUP BY OUTs.catid,OUTs.subcatid ) SELECT T1.CatID as CategoryID,T1.SubCatID as SubcategoryID,ALL_IN as QuantityIN,ALL_OUT as QuantityOUT, ALL_IN - ALL_OUT作爲剩餘量 FROM SUM_IN T1 LEFT JOIN SUM_OUT T2 ON T1.CatID = T2.CatID and T1.SubCatID = T2.SubCatID'像一個小調整的魅力工作...感謝你非常多! – 2013-02-18 14:55:49

1

您可以使用子查詢得到你想要的東西,例如:

SELECT * 
FROM (SELECT INs.catid   AS CategoryID, 
      INs.subcatid  AS SubcategoryID, 
      SUM(INs.quantity) AS QuantityIN 
    FROM in_stock INs 
    GROUP BY INs.catid, 
       INs.subcatid) AS a 
    LEFT JOIN (SELECT OUTs.catid   AS CategoryID, 
        OUTs.subcatid  AS SubcategoryID, 
        SUM(OUTs.quantity) AS QuantityOUT 
       FROM out_stock OUTs 
       GROUP BY OUTs.catid, 
         OUTs.subcatid) AS b 
      ON (a.subcategoryid = b.subcategoryid) 
      AND (a.categoryid = b.categoryid); 

由此看來,這是很容易的確編輯和修改使用查詢設計窗口查詢在MS Access

SELECT a.categoryid, 
    a.subcategoryid, 
    a.quantityin, 
    b.quantityout, 
    [quantityin] - [quantityout] AS RemainingQuantity 
FROM (SELECT INs.catid   AS CategoryID, 
      INs.subcatid  AS SubcategoryID, 
      SUM(INs.quantity) AS QuantityIN 
    FROM in_stock INs 
    GROUP BY INs.catid, 
       INs.subcatid) AS a 
    LEFT JOIN (SELECT OUTs.catid   AS CategoryID, 
        OUTs.subcatid  AS SubcategoryID, 
        SUM(OUTs.quantity) AS QuantityOUT 
       FROM out_stock OUTs 
       GROUP BY OUTs.catid, 
         OUTs.subcatid) AS b 
      ON (a.subcategoryid = b.subcategoryid) 
      AND (a.categoryid = b.categoryid); 
+0

哇,這很快......迄今爲止**是很好的,但實際上我感興趣的是RemaningQuantity collumn。 – 2013-02-18 14:33:45

+0

使用查詢設計窗口可以輕鬆地添加剩餘數量,實際上,您現在可以通過設計窗口和上面的SQL控制過濾器和字段。 – Fionnuala 2013-02-18 15:05:46