Skip to content
Advertisement

AVG in SQL Server

I am learning SQL Server, and I have a sample database which I got from my co-worker.

I created a stored procedure previously which works fine. Now I am trying to show average of the column which I am struggling at the moment.

Your supports means a lot and I will learn from this.

So here is my query:

And my table is like

And Now I want to show the avg rating based on the name and techno column such that my table should be like

Thanks in advance

Advertisement

Answer

You would need to adapt the group by clause to generate the one row per name and techno, and use an aggregate function to compute the rating:

Important notes:

  • Use standard joins! Implicit joins (with commas in the from clause and joining conditions in the where clause) are legacy syntax from decades ago, that should be used in new code

  • Do not use single quotes as quoting character for identifiers; use the relevant quoting character for your platform (in SQL Server, square brackets) – or better yet, use identifiers that do not require quoting

  • If rating is an integer, you need to turn it to a decimal before averaging it (otherwise you get an integer average)

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement