title: SQL Server : inner joins and group by
body: <p>I have a query that counts the number of enquiries made by a phone operator per day</p>
  
  <pre><code>SELECT 
     [OperatorID],
     DATEADD(day, DATEDIFF(day, 0, CreationDate), 0) AS CreationDate,
     COUNT(*) AS EnquiryCount
  FROM 
     [Enquiries] AS e
  GROUP BY 
     [OperatorID], DATEADD(day, DATEDIFF(day, 0, CreationDate), 0)
  </code></pre>
  
  <p>However, I want to add the <code>Operator</code> table to this query so that I can retrieve the operators name</p>
  
  <pre><code>SELECT [OperatorID]
    ,[FirstName]
    ,[LastName]
  FROM [Operators]
  </code></pre>
  
  <p>If I inner join the 2 tables before the group by clause:</p>
  
  <pre><code>SELECT [OperatorID]
    , DATEADD(day, DATEDIFF(day, 0, CreationDate), 0) AS CreationDate
    , COUNT(*) AS EnquiryCount
    , st.FullName
  FROM [Enquiries] AS e
  
  INNER JOIN
  
  (SELECT     OperatorID, FirstName + ' ' + LastName AS FullName 
  FROM          dbo.Operators
  WHERE      (Role = 'Operator')) AS o ON e.OperatorID = o.OperatorID
  
  GROUP BY [OperatorID], DATEADD(day, DATEDIFF(day, 0, CreationDate), 0)
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>Column 'o.FullName' is invalid in the select list because it is not
    contained in either an aggregate function or the GROUP BY clause.</p>
  </blockquote>
  
  <p>How do I get around this?</p>