Selecting DISTINCT

  • I have three fields I am interested in:

    Employee_ID,

    Employee_Title,

    Employee_Entry

    I want to be able to select these three fields but only the record with the most recent Employee_Entry date. In some cases an Employee may have moved job titles and have several entries against the Employee_ID.

    I have started with something like this:

    SELECT DISTINCT

    Employee_ID,

    Employee_Title,

    Employee_Entry

    FROM emp.employees

    I have tried using Max(Employee_Entry) but this returns all records for those who have multiple titles. I just want the current most recent Employee_title.

  • i think using the row number with a sub select will get you what you are after:

    SELECT

    Employee_ID,

    Employee_Title,

    Employee_Entry

    FROM (SELECT

    ROW_NUMBER() OVER (partition BY Employee_ID ORDER BY Employee_Entry DESC) AS RW,

    Employee_ID,

    Employee_Title,

    Employee_Entry

    FROM emp.employees) myAlias

    WHERE RW = 1

    Lowell


    --help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!

  • That did it.

    Thank you.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply