Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9205405
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:56:35+00:00 2026-06-17T23:56:35+00:00

I am having trouble writing a query that will select all Skills, joining the

  • 0

I am having trouble writing a query that will select all Skills, joining the Employee and Competency records, but only return one skill per employee, their newest Skill. Using this sample dataset

Skills
======
id   employee_id   competency_id   created
1    1             1               Jan 1
2    2             2               Jan 1
3    1             2               Jan 3

Employees
===========
id   first_name   last_name
1    Mike         Jones
2    Steve        Smith


Competencies
============
id   title
1    Problem Solving
2    Compassion

I would like to retrieve the following data

Skill.id   Skill.employee_id   Skill.competency_id   Skill.created   Employee.id   Employee.first_name   Employee.last_name   Competency.id   Competency.title
2          2                   2                     Jan 1           2             Steve                 Smith                2               Compassion
3          1                   2                     Jan 3           1             Mike                  Jones                2               Compassion

I was able to select the employee_id and max created using

SELECT MAX(created) as created, employee_id  FROM skills GROUP BY employee_id

But when I start to add more fields in the select statement or add in a join I get the ‘Column ‘xyz’ is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.’ error.

Any help is appreciated and I don’t have to use GROUP BY, it’s just what I’m familiar with.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T23:56:35+00:00Added an answer on June 17, 2026 at 11:56 pm

    The error that you were getting is because SQL Server requires any item in the SELECT list to be included in the GROUP BY if there is an aggregate function being used.

    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:

    You can use a subquery to get this result. This gets the max(created) in a subquery and then you use that result to get the correct employee record:

    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
    

    See SQL Fiddle with Demo

    Or another way to do this is to use row_number():

    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
    

    See SQL Fiddle with Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble writing query so that I can query the content of
I'm having trouble writing an XPath expression to select nodes that contain certain elements,
I am having trouble writing a sql query and I was hoping that someone
I'm new to linq and I'm having trouble writing a query that pulls back
I am having trouble writing a query to select one row per date, given
I am having trouble writing a query. I have been working with UNION on
I am having some trouble with writing a unit test that checks my custom
I am having trouble writing a query on a mysql table of user activity
I am having trouble writing this query. I need to get the current number
I am having trouble writing a query to get the results I want. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.