title: SQL Server : get count and sum of columns
body: <p>I am designing a website that people post text messages. users can rate the messages (1 to 5). I'm recording this ratings into a table to:</p>
  
  <ol>
  <li>avoid multiple rating by one user for the same post</li>
  <li>to be able get the average rating of a post.</li>
  </ol>
  
  <p>but I also the number of votes are important too. for example a post having 10 votes of 3 (makes 30) are valuable than the post with 2 votes of 5 (that makes 10). Now when I'm showing a page with 20 of this messages I want to show their rating too</p>
  
  <p>This is my rating table:</p>
  
  <pre><code>create table ratings(
  [id] int primary key identity(1,1),
  [msgid] int,
  [uid] int,
  [rating] tinyint
  )
  </code></pre>
  
  <p>I want to get to 20 messages from messages table with the sum of the ratings for each message and the count of ratings for each message.</p>
  
  <pre><code>select 
      msg.id, msg.text,
      sum(rate.rating), 
      count(rate.id) 
  from 
      messages as msg 
  inner join 
      ratings as rate on msg.id = rate.msgid 
  group by 
      msg.id
  </code></pre>
  
  <p>can any one please help me with the SQL query code?</p>