2017-08-24 107 views
0

我有SQL这样的代码时,选择所有列使用INNER JOIN

Msg 8120, Level 16, State 1, Line 3
Column 'TB_DataProperti.Kode_Properti' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

我只是想选择使用*所有数据列,因为我有很多列

+1

1'*'是邪恶的。不要使用它。 2.请参阅https://meta.stackoverflow.com/questions/333952/why-should-i-provide-an-mcve-for-what-seems-to-me-to-be-a-very-simple-sql - 查询 – Strawberry

+4

这个错误不是由内部连接引起的,它是由组引起的。当你使用group时,你只能选择group by或aggregate函数中的列(min/max/avg/..) – Luc

回答

0

问题是您正在尝试使用一个表的聚合函数和另一个表上的group by。规则是如果您正在使用另一列的聚合函数该列应该用于group.Still试试这个,我希望这是有用的。

SELECT 
     TB_DataProperti.*, 
     ISNULL(AVG(TBL_Rating.Rating), 0) over (partition by TBL_Rating.Kode_Properti) as AverageRating 
    FROM 
     TB_DataProperti 
    INNER JOIN 
     TBL_Rating ON TB_DataProperti.Kode_Properti = TBL_Rating.Kode_Properti 
    ORDER BY 
     AverageRating DESC 
+0

谢谢兄弟..对我有用.. –