troubleshooting Question

SQL Syntax for Update by Ranking by Quote and Customer

Avatar of rwheeler23
rwheeler23Flag for United States of America asked on
Microsoft SQL ServerSQL
3 Comments1 Solution63 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Brian Crowe
Database Manager

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Top Expert 2005

The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros