有没有更好的方法来编写此选择查询

时间:2016-04-27 11:16:04

标签: sql sql-server

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 

我得到了外面的

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

现在,我将获取所有10条记录的项目代码11的重复记录为2。 我希望Y值映射到项目代码11本身,所有其他值应为null

2 个答案:

答案 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。

关于这个主题,有一些很好的资源;

答案 1 :(得分:3)

您在派生表t1和t2之间没有指定JOIN。如果您希望得到t1的结果,而不是每行的t2,则需要使用LEFT JOIN。

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;