关键字' AS'附近的语法不正确,试图找到总和

时间:2015-06-23 18:20:30

标签: sql sql-server sum

我检查了与标题类似的其他问题,但找不到答案。我正在通过一个软件,所以我无法直接调整SQL,我只能更改某些部分。

  

[Microsoft] [ODBC SQL Server驱动程序] [SQL Server]关键字' AS'附近的语法不正确。

代码:

SET NOCOUNT ON; 
DECLARE @ShowZeros nVarChar(4000);

SET @ShowZeros  = 'N';

SELECT 
    IC_ProductLots.Available_Alt
    , IC_Products.UnitOfMeasure_Alt
    , IC_ProductLots.Available_Stk
    , IC_Products.UnitOfMeasure_Stk
    , IC_Products.Description1
    , IC_Products.ProductCode
    , AR_Customers.CustomerCode
    , AR_Customers.Name
    , SUM([IC_ProductLots].[Available_Stk]) GROUP BY [AR_Customers].[CustomerCode] AS [Total Stock]
FROM ((( DC_Transactions 
    INNER JOIN  AR_Customers 
        ON DC_Transactions.CustomerKey = AR_Customers.CustomerKey)
    INNER JOIN  IC_ProductLots 
        ON DC_Transactions.LotKey = IC_ProductLots.LotKey)
    INNER JOIN  IC_Products 
        ON DC_Transactions.ProductKey = IC_Products.ProductKey)
WHERE 
    (IC_Products.ProductCode = '      515070')  
    AND (
        (
            CASE 
                WHEN @ShowZeros = 'Y' or @ShowZeros = 'YES' THEN 1 
                ELSE (ISNULL([IC_ProductLots].[Available_Stk],0))
            END
        ) > 0
    )
ORDER BY 
    IC_Products.ProductCode 
  , AR_Customers.CustomerCode 
  , AR_Customers.Name

我试图找到可用库存的总和,我怎么遇到我的Select sum语句的问题:

 SUM([IC_ProductLots].[Available_Stk]) 
 GROUP BY [AR_Customers].[CustomerCode] AS [Total Stock]

找到了答案,但我的输出仍不正确, 它应该如下所示: 输出应类似于以下内容:

Cases | U/M  | Total Stock | Description | Cust Name  | Cust Code
-----------------------------------------------------------------
57    | CS   | 1779.45     | Food        | Restaurant | 2
4     | CS   | 120         | Dough       | Bakery     | 44

目前,它为客户代码2打印出57行,为客户代码44打印出4行,为每个客户代码显示相同的信息。基本上它会为客户的每个案例打印出一条线。

2 个答案:

答案 0 :(得分:2)

您需要在Where子句之后列出Group By子句中未包含在聚合函数中的所有列

所以你的查询将是

       SELECT 
     IC_ProductLots.Available_Alt
    , IC_Products.UnitOfMeasure_Alt
    , IC_ProductLots.Available_Stk
    , IC_Products.UnitOfMeasure_Stk
    , IC_Products.Description1
    , IC_Products.ProductCode
    , AR_Customers.CustomerCode
    , AR_Customers.Name
    , SUM([IC_ProductLots].[Available_Stk])  AS [Total Stock]
     FROM ((( DC_Transactions 
        INNER JOIN  AR_Customers 
         ON DC_Transactions.CustomerKey = AR_Customers.CustomerKey)
        INNER JOIN  IC_ProductLots 
         ON DC_Transactions.LotKey = IC_ProductLots.LotKey)
        INNER JOIN  IC_Products 
         ON DC_Transactions.ProductKey = IC_Products.ProductKey)
     WHERE 
        (IC_Products.ProductCode = '      515070')  AND 
         ((CASE WHEN @ShowZeros = 'Y' or @ShowZeros = 'YES' THEN 1 ELSE
     (ISNULL([IC_ProductLots].[Available_Stk],0))
        END) > 0)
GROUP BY IC_ProductLots.Available_Alt
    , IC_Products.UnitOfMeasure_Alt
    , IC_ProductLots.Available_Stk
    , IC_Products.UnitOfMeasure_Stk
    , IC_Products.Description1
    , IC_Products.ProductCode
    , AR_Customers.CustomerCode
    , AR_Customers.Name
       ORDER BY 
        IC_Products.ProductCode 
      , AR_Customers.CustomerCode 
      , AR_Customers.Name

答案 1 :(得分:1)

也许您尝试使用窗口功能。如果这是您的意图,您可以改为使用此代码:

SET NOCOUNT ON; 
DECLARE @ShowZeros nVarChar(4000);

SET @ShowZeros  = 'N';

SELECT 
 IC_ProductLots.Available_Alt
, IC_Products.UnitOfMeasure_Alt
, IC_ProductLots.Available_Stk
, IC_Products.UnitOfMeasure_Stk
, IC_Products.Description1
, IC_Products.ProductCode
, AR_Customers.CustomerCode
, AR_Customers.Name
, SUM([IC_ProductLots].[Available_Stk]) OVER(PARTITION BY [AR_Customers].[CustomerCode]) AS [Total Stock]
 FROM ((( DC_Transactions 
    INNER JOIN  AR_Customers 
     ON DC_Transactions.CustomerKey = AR_Customers.CustomerKey)
    INNER JOIN  IC_ProductLots 
     ON DC_Transactions.LotKey = IC_ProductLots.LotKey)
    INNER JOIN  IC_Products 
     ON DC_Transactions.ProductKey = IC_Products.ProductKey)
 WHERE 
    (IC_Products.ProductCode = '      515070')  AND 
     ((CASE WHEN @ShowZeros = 'Y' or @ShowZeros = 'YES' THEN 1 ELSE
 (ISNULL([IC_ProductLots].[Available_Stk],0))
    END) > 0)
   ORDER BY 
    IC_Products.ProductCode 
  , AR_Customers.CustomerCode 
  , AR_Customers.Name

选择中的组是错误的。我将其更改为OVER - 子句