SQL Syntax for Update by Ranking by Quote and Customer

I was working on a script to mark old quotes as inactive but I ran into this bump. Quotes can be duplicated for the same customer or for a different customer. My current script works fine assuming the customer number is the same but breaks down if the customer number is different. For example:

CUSTOMER_NUMBER    QUOTE_NUMBER   SEQUENCE_NUMBER
100,20150100001,0
100,20150100001,1
100,20150100001,2

In this case the first two quotes need to be set to inactive because when the quote was duplicated it was for the same customer.

CUSTOMER_NUMBER    QUOTE_NUMBER   SEQUENCE_NUMBER
100,20150100001,0
101,20150100001,1
102,20150100001,2
100,20150100001,3

In this case, only the first quote would be set to inactive because there is a higher sequence number for the same quote number and customer number. Quotes two and three are not to be set to inactive because even though the quote numbers are the same the customer number is different.

I tried changing the ranking to include the customer number but it still updates the same number of records. How do I update this script to only set duplicate quotes for the same customer?

===============================================================================================================
UPDATE CSTQUTHD
SET INACTIVE=1
FROM CSTQUTHD T1
INNER JOIN
(
  SELECT
            QUOTE_NUMBER
          , SEQUENCE_NUMBER
          , REVALIDATE_NUMB
          , ROW_NUMBER() OVER (PARTITION BY QUOTE_NUMBER
                              ORDER BY SEQUENCE_NUMBER DESC) AS VersionRank
  FROM CSTQUTHD
) as T2
  ON T1.QUOTE_NUMBER=T2.QUOTE_NUMBER AND T1.SEQUENCE_NUMBER=T2.SEQUENCE_NUMBER AND T1.REVALIDATE_NUMB=T2.REVALIDATE_NUMB
  WHERE T2.VersionRank>1
LVL 1
rwheeler23Asked:
Who is Participating?
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:
You need to PARTITION BY customer and Quote not just Quote_Number.  If you could post some sample data from your source table I could provide tested sample code.

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
rwheeler23Author Commented:
Thanks you are correct. I had only put the customer after the partition command and not the order by. Once I put it in both places the update command worked fine.
rwheeler23Author Commented:
Thanks. Sometimes you just need a second pair of eyes. Now I can move onto my next headache.
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.