2

谁能指出我在哪里弄乱了下面的查询以及如何纠正它。

我得到的错误是:

SQL Server 数据库错误:选择列表中的“A.competitor_id”列无效,因为它既不包含在聚合函数中,也不包含在 GROUP BY 子句中。第 1 行第 0 列

SELECT A.competitor_id, A.Competitor_Name,    

(((AVG(A.Final_Price_Shipping)/AVG(D.Final_Price_Shipping))*100)-100) as [Final Price Shipping Index],                  
  (((AVG(A.Final_Price)/AVG(D.Final_Price))*100)-100) as [Final Price Index],                   
  (((AVG(A.Regular_price)/AVG(D.Regular_price))*100)-100) as [Regular Price Index]                  

from 


(SELECT A.competitor_id,C.Competitor_Name, COUNT(1) OVER (PARTITION BY  A.competitor_id) as Row_Count,
avg(Final_Price) Final_Price,avg(Final_Price_Shipping) Final_Price_Shipping,
avg(Regular_price) as Regular_price  ,SUM(A.Competitor_id) as cnt_NL
from [Tb_Competitor_Product_Info_New] A with (nolock)
inner join Tb_Competitor C on C.Competitor_Id=A.Competitor_ID
 where A.Competitor_id in (1)
  group by A.Competitor_ID,C.Competitor_Name) As A,




  (SELECT A.competitor_id, COUNT(1) OVER (PARTITION BY  A.competitor_id) as Row_Count,
avg(Final_Price) Final_Price,avg(Final_Price_Shipping) Final_Price_Shipping,
avg(Regular_price) as Regular_price  ,SUM(A.Competitor_id) as cnt_NL
from [Tb_Competitor_Product_Info_New] A with (nolock)
 where A.Competitor_id in (2,3,4,5,6,7,8,9)
  group by A.Competitor_ID ) As D 
4

1 回答 1

1

只需将 GROUP BY 添加到查询的末尾,我猜你的 A 和 D 表也必须连接:

SELECT A.competitor_id, A.Competitor_Name,    

(((AVG(A.Final_Price_Shipping)/AVG(D.Final_Price_Shipping))*100)-100) 
    as [Final Price Shipping Index],                  
(((AVG(A.Final_Price)/AVG(D.Final_Price))*100)-100) as [Final Price Index],                   
(((AVG(A.Regular_price)/AVG(D.Regular_price))*100)-100) 
    as [Regular Price Index]                  

from 
(.....) As A
INNER JOIN (.....) As D on A.competitor_id=D.competitor_id

GROUP BY A.competitor_id, A.Competitor_Name
于 2013-10-21T05:54:36.270 回答