title: SQL Query Error in Group By and Order By Clause
body: <p>I am getting chat messages of a user from two tables so I use an <code>inner join</code> to this. Here is my SQL query:</p>
  
  <pre><code>SELECT  
      m.Message_Id 
      , m.Chat_Relation_Id AS relation_id 
      , m.SenderId AS datetime 
      , m.Text_Message 
      , m.Subject 
      , m.DateTime
  FROM  
      [AppResource].[dbo].[ChatMessage] AS m  
      INNER JOIN [AppResource].[dbo].[chatRelation] AS r 
      ON r.Chat_Relation_Id = m.Chat_Relation_Id 
  WHERE  
      r.User1Id=1 
  GROUP BY 
      m.Chat_Relation_Id 
  ORDER BY  
      m.DateTime DESC
  </code></pre>
  
  <p>But when I am running this, I am getting the following error message: </p>
  
  <blockquote>
    <p>Column 'AppResource.dbo.ChatMessage.Message_Id' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.</p>
  </blockquote>
  
  <p>I have searched a lot on google and found many solutions but nothing works. <code>Text_Message</code> is  of <code>text</code> datatype and <code>DateTime</code> is of <code>Datetime</code> datatype.</p>
  
  <p>Please help me in it.</p>
  
  <pre><code>Message_Id  relation_id datetime    Text_Message    Subject DateTime
  4   1   2   Hello   Hi  2014-11-25 00:00:00.000
  8   25  1   hiii    ho  2014-03-26 09:07:27.000
  7   26  1       hiiiiii 2014-03-26 07:07:27.000
  6   25  1       hi  2014-03-26 07:01:58.000
  </code></pre>