2012-11-27 72 views
1
select tblProductMaster.*,AVG(tblReviewMaster.Rating) from tblProductMaster 
    left join tblReviewMaster 
    on tblProductMaster.ProductID = tblReviewMaster.ProductID  
Group By tblProductMaster.ProductID  

該查詢返回錯誤:在SQL Server 2008

Column 'tblReviewMaster.ReviewID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

這不是讓我有AVG功能的任一列...如果我寫

select AVG(tblReviewMaster.Rating) from tblProductMaster ... 

然後它工作正常

那麼如何從tblProductMaster獲得產品詳細信息?

回答

0

錯誤消息稱「列tblReviewMaster.ReviewID「是因爲它未包含在聚合函數或GROUP BY子句的選擇列表中無效。

這就意味着所有的比其他的列集合函數(AVG)中的列應該是分組依據的一部分。因此,如果*表示(第1列,第2列....等),所有這些列必須出現在GroupBy子句中。

所以,你的查詢會

select tblProductMaster.*,AVG(tblReviewMaster.Rating) from tblProductMaster 
    left join tblReviewMaster 
    on tblProductMaster.ProductID = tblReviewMaster.ProductID  
Group By tblProductMaster.* 

由*,我的意思寫的,代表所有列*。 *在這裏不起作用。

請找到更多的細節上MSDN

+0

感謝答覆,它工作正常..ü救了我太多的時間.. :) –

+0

@shivanipatel:這是我們推薦#2接受的答案,如果它是有用的並且是您的問題的最佳答案。這有助於其他人尋找解決方案。 –

+0

對不起,我是新來的stakoverflow,所以我知道它.. –