SELECTing the top row when INSERTING

I have a table 'Matters'.  This has unique rows.
I have a table 'MattersProfessional'.  There will be multiple rows in 'MattersProfessional' for each unique 'pkMatterID '.

Matters Table:
pkMatterID
1
2
3

Before INSERT INTO MattersProfessionals Table:
pkMattersProfessionals           fkMatterID           fkProfessionalID               AssignedType                 AssignedPercent
1234                                                       1                             87865                                  Responsible                             100
2134                                                       1                               6543                                 Originator                                100
3134                                                        2                               7654                                  Responsible                               60
4134                                                         2                              3345                                Responsible                                40
5134                                                         2                                9878                             Originator                                  100
6134                                                          3                                  1345                            Responsible                               40
7134                                                          3                               23412                                Responsible                                10
8134                                                          3                               87656                                   Responsible                                10


After INSERT INTO MattersProfessionals Table:
pkMattersProfessionals           fkMatterID           fkProfessionalID               AssignedType                 AssignedPercent
1234                                                       1                             87865                                  Responsible                             100
2134                                                       1                               6543                                 Originator                                100
3134                                                        2                               7654                                  Responsible                               60
4134                                                         2                              3345                                Responsible                                40
5134                                                         2                                9878                             Originator                                  100
6134                                                          3                                  1345                            Responsible                               40
7134                                                          3                               23412                                Responsible                                10
8134                                                          3                               87656                                   Responsible                                10
9134                                                        1                                   87865                            Billing Atty                                   100
10134                                                       2                                  7654                                  Billing Atty                                    100
10134                                                       3                                  1345                                  Billing Atty                                    100


INSERT INTO MattersProfessionals
  (pkMattersProfessionals, fkMatterID, AssignedType, AssignedPercent)  

SELECT newID(), m.matters, 'Billing Atty', 100
  FROM Matters m
  JOIN MattersProfessionals mp ON m.pkMatterID= mp.fkMatterID
  WHERE  AssignedType = 'Responsible'

Open in new window


This query is fine but how do I add the fkProfessionalID with the AssignedType = 'Responsible' that has the highest AssignedPercent?

I hope I gave enough detail.
huerita37Asked:
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.

Brian CroweDatabase AdministratorCommented:
I didn't see a place in Matters table to put the fkProfesionalID so I just assumed there was a column of the same name.

WITH cteMattersProfessional AS
(
   SELECT pkMattersProfessionals, fkMatterID, fkProfessionalID, AssignedType, AssignedPercent,
      ROW_NUMBER() OVER(PARTITION BY fkProfessionalID ORDER BY AssignedPercent DESC) AS RowNumber
   FROM MattersProfessional
   WHERE AssignedType = 'Responsible'
)
UPDATE Matters
SET fkProfessionalID = MP.fkProfessionalID
FROM ctemattersProfessional AS MP
INNER JOIN Matters AS M
   ON MP.fkMatterID = M.pkMatterID
   AND MP.RowNumber = 1

Open in new window

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
huerita37Author Commented:
Thank you very much.  I've seen 'OVER (PARTITION...' recently.  Is this similar to GROUP BY?
Brian CroweDatabase AdministratorCommented:
It is called a Window function.  Here is a good write up on them.

https://www.simple-talk.com/sql/t-sql-programming/window-functions-in-sql/
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
Microsoft SQL Server

From novice to tech pro — start learning today.