0

我正在尝试添加一个计算总收入百分比的列,但遇到以下错误:

错误:消息 207,级别 16,状态 1,第 14 行无效的列名称“客户密钥”。

在该行中,我试图加入表 1 和表 3,但T.Customerkey即使dbo.FactInternetSales表中存在 customerkey,MS SQL Server 也无法识别。此外,当我在 Group By 子句中添加 T.Grand_Tot_Rev 时,它会为每一行返回 0.04。我知道这是错误的,因为我不想T.Grand_Tot_Rev成为聚合的一部分,因为它应该对每条记录保持不变。我怎样才能实现我正在寻找的目标?先感谢您。顺便说一句,我使用的是 AdventureWorksDW2012 数据库。

SELECT fs.CustomerKey , 
       M.Total_sales , 
       M.Total_cost , 
       M.Total_sales - M.Total_cost AS Total_Margin , 
       T.Grand_Tot_Rev( M.Total_sales / T.Grand_Tot_Rev ) * 100 AS Prct_Total_Revenue
  FROM dbo.FactInternetSales fs , -- Table 1 --

( 
  SELECT customerkey , 
         SUM( SalesAmount )AS Total_Sales , 
         SUM( TotalProductCost )Total_cost
    FROM dbo.FactInternetSales
    GROUP BY customerkey 
) M , --Table 2 --

( 
  SELECT SUM( SalesAmount )AS Grand_Tot_Rev
    FROM dbo.FactInternetSales 
) T --Table 3 --

  WHERE fs.CustomerKey =  M.CustomerKey -- Join 1 --     
    AND M.CustomerKey = T.Customerkey   -- Join 2 -- 
  GROUP BY fs.CustomerKey , 
           M.Total_sales , 
           M.Total_cost , 
           T.Grand_Tot_Rev
  ORDER BY 2 DESC;
4

1 回答 1

1

如果您希望 T.Grand_Tot_Rev 作为所有行的常量,请尝试删除第二个连接AND M.CustomerKey = T.Customerkey -- Join 2 --,以便查询如下所示:

SELECT fs.CustomerKey , 
       M.Total_sales , 
       M.Total_cost , 
       M.Total_sales - M.Total_cost AS Total_Margin , 
       T.Grand_Tot_Rev,
      ( M.Total_sales / T.Grand_Tot_Rev ) * 100 AS Prct_Total_Revenue
  FROM dbo.FactInternetSales fs , -- Table 1 --

( 
  SELECT customerkey , 
         SUM( SalesAmount )AS Total_Sales , 
         SUM( TotalProductCost )Total_cost
    FROM dbo.FactInternetSales
    GROUP BY customerkey 
) M , --Table 2 --

( 
  SELECT SUM( SalesAmount )AS Grand_Tot_Rev
    FROM dbo.FactInternetSales
) T --Table 3 --

  WHERE fs.CustomerKey =  M.CustomerKey -- Join 1 --     
    --AND M.CustomerKey = T.Customerkey    -- Join 2 -- 
  GROUP BY fs.CustomerKey , 
           M.Total_sales , 
           M.Total_cost , 
           T.Grand_Tot_Rev
  ORDER BY 2 DESC;

另一种编写相同查询的方法更紧凑,性能可能稍好一些:

;WITH 
    T AS (
        SELECT SUM(SalesAmount) AS Grand_Tot_Rev  
        FROM dbo.FactInternetSales
    ),
    M AS (
       SELECT customerkey , 
        SUM(SalesAmount) AS Total_Sales , 
        SUM(TotalProductCost) AS Total_cost
       FROM dbo.FactInternetSales
       GROUP BY CustomerKey
)

SELECT 
    customerkey , 
    Total_Sales , 
    Total_cost,
    Total_Sales - Total_cost AS Total_Margin , 
    Grand_Tot_Rev,
    Total_Sales / Grand_Tot_Rev * 100 AS Prct_Total_Revenue   
FROM M, T
ORDER BY 2 DESC;

要查看非常小的值,您可以强制转换为更广泛的数据类型:

;WITH 
    T AS (
        SELECT CAST(SUM(SalesAmount) AS decimal) AS Grand_Tot_Rev  
        FROM dbo.FactInternetSales
    ),
    M AS (
       SELECT customerkey , 
        CAST(SUM(SalesAmount) AS decimal(15,10)) AS Total_Sales , 
        CAST(SUM(TotalProductCost) AS decimal(15,10)) AS Total_cost
       FROM dbo.FactInternetSales
       GROUP BY CustomerKey
)

SELECT 
    customerkey , 
    Total_Sales , 
    Total_cost,
    Total_Sales - Total_cost AS Total_Margin , 
    Grand_Tot_Rev,
    Total_Sales / Grand_Tot_Rev * 100 AS Prct_Total_Revenue   
FROM M, T
ORDER BY 2 DESC;
于 2014-10-13T21:43:43.830 回答