Select All Columns When Using Inner Join [Duplicate]

Select All Columns When Using Inner Join [Duplicate]

I have SQL code like this :

SELECT
    TB_DataProperti.*,
    TBL_Rating.ISNULL(AVG(Rating), 0) AverageRating
FROM 
    TB_DataProperti
INNER JOIN 
    TBL_Rating ON TB_DataProperti.Kode_Properti = TBL_Rating.Kode_Properti
GROUP BY 
    TB_DataProperti.JudulListing
ORDER BY 
    AverageRating DESC

I get this error:

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.

I just want to select all data columns using *, because I have many columns

2

1 Answer

Problem is You are trying to use aggregate function of one table and group by on another table.The rule is if you are using aggregate function with another column then that column should use in group by.Still try this I hope this is useful.

  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
1
Marcus Vance
Author

Marcus Vance

Marcus Vance is a cybersecurity auditor and technology writer dedicated to educating the public about online safety, data privacy regulations, enterprise security, and emerging cyber threats.