Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The error that you were getting is because SQL Server requires any item in the <code>SELECT</code> list to be included in the <code>GROUP BY</code> if there is an aggregate function being used. </p> <p>The problem with that is you might have unique values in some columns which can throw off the result. So you will want to rewrite the query to use one of the following:</p> <p>You can use a subquery to get this result. This gets the <code>max(created)</code> in a subquery and then you use that result to get the correct employee record:</p> <pre><code>select s.id SkillId, s.employee_id, s.competency_id, s.created, e.id employee, e.first_name, e.last_name, c.id competency, c.title from Employees e left join Skills s on e.id = s.employee_id inner join ( SELECT MAX(created) as created, employee_id FROM skills GROUP BY employee_id ) s1 on s.employee_id = s1.employee_id and s.created = s1.created left join Competencies c on s.competency_id = c.id </code></pre> <p>See <a href="http://sqlfiddle.com/#!3/e2259/12" rel="nofollow">SQL Fiddle with Demo</a></p> <p>Or another way to do this is to use <code>row_number()</code>:</p> <pre><code>select * from ( select s.id SkillId, s.employee_id, s.competency_id, s.created, e.id employee, e.first_name, e.last_name, c.id competency, c.title, row_number() over(partition by s.employee_id order by s.created desc) rn from Employees e left join Skills s on e.id = s.employee_id left join Competencies c on s.competency_id = c.id ) src where rn = 1 </code></pre> <p>See <a href="http://sqlfiddle.com/#!3/e2259/13" rel="nofollow">SQL Fiddle with Demo</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload