MSSQL - Analytic function help

Please see the attached witch shows

fig1. current output from tbl_assessments
fig2. desired output from the same table

I want the output to show an additional column (first_assessment_id). This will get the assessment ID from the assessment with the earliest end_date for the person and repeat it for every subsequent assessment for the person. I tried something like

min(ssca_id) over (partition by ssca_dim_person_id order by ssca_dim_person_id, ssca_end_date asc)

but found this didn’t work as the earliest assessment_id recorded for the person was not always a lower number than subsequent assessments for the person

any help is appreciated.
sample-data-20181025.xlsx
tim_BurgessAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

lcohanDatabase AnalystCommented:
you will need to do something like:
SELECT MIN( ssca_end_date  ) AS ssca_end_date , ssca_id
FROM YourTable
GROUP BY ssca_id

Open in new window


and make sure indexes exist on both columns - ssca_end_date in particular to support implicit sorting needed by MIN function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tim_BurgessAuthor Commented:
thankyou
awking00Information Technology SpecialistCommented:
If you are using sql server 2012 or later, you can us the first_value() function to get what you want -
select first_value(assessment_id) over (partition by person_name order by end_date) first_assessment_id, assessment_id,
person_id, person_name, end_date
from yourtable;
tim_BurgessAuthor Commented:
thankyou awking00, thats a neat solution
awking00Information Technology SpecialistCommented:
uw :-)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Query Syntax

From novice to tech pro — start learning today.