2016-04-27 185 views
1
SELECT t1.X, 
     t1.Item_Code, 
     t2.X, 
     t2.Item_Code 
FROM (SELECT Sum(e.item_qty) AS X, 
       A.Item_Code 
     FROM [Item Master]A 
       INNER JOIN [Counter Issue Details] e 
         ON e.Item_Code = A.Item_Code 
       INNER JOIN [Counter Master] CM 
         ON CM.Counter_Code = e.Counter_Code 
     WHERE e.Item_Code = A.Item_Code 
       AND A.Type_Code = 0 
     GROUP BY A.Item_Code)t1, 
     (SELECT Sum(e.item_qty) AS X, 
       A.Item_Code 
     FROM [Item Master]A 
       INNER JOIN [Counter Return Details] e 
         ON e.Item_Code = A.Item_Code 
       INNER JOIN [Counter Master] CM 
         ON CM.Counter_Code = e.Counter_Code 
     WHERE e.Item_Code = A.Item_Code 
       AND A.Type_Code = 0 
     GROUP BY A.Item_Code)t2 

蔭得到OUTPT已有沒有更好的方式來寫這個選擇查詢

X          Item_Code        Y          Item_Code 
--------------------------------------- --------------------------------------- --------------------------------------- --------------------------------------- 
3998.000        1          2.00         11 
2000.000        2          2.00         11 
2000.000        3          2.00         11 
2504.000        4          2.00         11 
2384.000        5          2.00         11 
2456.000        6          2.00         11 
2372.000        7          2.00         11 
4900.000        9          2.00         11 
2000.000        10          2.00         11 
3948.000        11          2.00         11 

現在我正在重複記錄的商品代碼11 2的所有10條記錄。 我希望將Y值映射到物品代碼11本身,其他所有物品都應該爲零

+0

請使用t1和t2之間的連接 –

+0

你想完成什麼?我可能會建議你問另一個問題(這個問題已經有答案),並解釋你在做什麼,提供樣本數據和期望的結果。 –

回答

5

您的問題是舊式笛卡爾連接充當CROSS JOIN。如果您考慮正確加入,您不會將數據翻倍;

SELECT t1.Item_Code 
    ,t1.X t1_x 
    ,ISNULL(t2.X,0) t2_x 
FROM (
    SELECT SUM(e.item_qty) AS X 
     ,A.Item_Code 
    FROM [Item Master] A 
    INNER JOIN [Counter Issue Details] e ON e.Item_Code = A.Item_Code 
    INNER JOIN [Counter Master] CM ON CM.Counter_Code = e.Counter_Code 
    WHERE e.Item_Code = A.Item_Code 
     AND A.Type_Code = 0 
    GROUP BY A.Item_Code 
    ) t1 

LEFT JOIN 
    (
     SELECT SUM(e.item_qty) AS X 
      ,A.Item_Code 
     FROM [Item Master] A 
     INNER JOIN [Counter Return Details] e ON e.Item_Code = A.Item_Code 
     INNER JOIN [Counter Master] CM ON CM.Counter_Code = e.Counter_Code 
     WHERE e.Item_Code = A.Item_Code 
      AND A.Type_Code = 0 
     GROUP BY A.Item_Code 
     ) t2 
ON t1.Item_Code = t2.Item_Code 

這將返回t1中的所有值和t2中的匹配信息。編輯:t2.X現在將顯示0,沒有數據而不是按要求填充NULL。

在這個問題上有一些很好的資源;

Which join syntax is better?

http://sqlblog.com/blogs/aaron_bertrand/archive/2009/10/08/bad-habits-to-kick-using-old-style-joins.aspx

+0

非常感謝Mr.Benner,它的工作原理非常完美。如果沒有我想要顯示的數據,請保存我的日子 – Venkatvasan

+0

,而不是顯示NULL。如何獲取此.. – Venkatvasan

+0

將選擇更改爲'ISNULL(t2.X, 0)t2_x'。我編輯了我的原始代碼以向您展示該示例。 –

3

你有沒有加入你們派生表T1和T2之間的規定。如果您期望t1的結果,則需要使用LEFT JOIN,而不是每行都使用t2。

SELECT t1.X 
     , t1.Item_Code 
     , t2.X 
     , t2.Item_Code 
FROM (SELECT SUM(e.item_qty) AS X 
        , A.Item_Code 
      FROM  [Item Master] A 
        INNER JOIN [Counter Issue Details] e ON e.Item_Code = A.Item_Code 
        INNER JOIN [Counter Master] CM ON CM.Counter_Code = e.Counter_Code 
      WHERE  e.Item_Code = A.Item_Code 
        AND A.Type_Code = 0 
      GROUP BY A.Item_Code 
     ) t1 
     LEFT JOIN (SELECT SUM(e.item_qty) AS X 
          , A.Item_Code 
        FROM [Item Master] A 
          INNER JOIN [Counter Return Details] e ON e.Item_Code = A.Item_Code 
          INNER JOIN [Counter Master] CM ON CM.Counter_Code = e.Counter_Code 
        WHERE e.Item_Code = A.Item_Code 
          AND A.Type_Code = 0 
        GROUP BY A.Item_Code 
       ) t2 ON t1.Item_Code = t2.Item_Code; 
相關問題