Avatar of brgdotnet
brgdotnet
Flag for United States of America

asked on 

Need help with a query involving flagging duplicate records.

I have a table named "Purchases" which has records related to  a customers purchases. Each record entry for a customer is uniquely identified by their Social Security number. attached is the scripted database.

I need a query that will peform the following below. Can someone please help me out?

Set the "Active" flag to 1 where the DUP_PARAM = 2 and the MoneyAmount is the largest value (Max) for any one customer.
For those customer records that do not have the Max MoneyAmount, and do have DUP_PARAM=2, then set the ActiveFlag = 9.
Also for the records that do not have DUP_Param = 2, then just ignore those records.

Purchases (Before Query) :

SocialSSN    DUP_Param    MoneyAmount  Active
888667000     2                     100.00                  0
888667000     2                    120.00                   0
888667000     2                    125.00                   0
888667000     0                    175.00                   0
787994098     2                   100.00                    0
787994098     2                   120.00                    0
555443000     1                   45.00                      0
555443000     2                   40.00                      0
474747474     2                   40.00                      0
474747474     2                   40.00                      0

Purchases (After Query) :

SocialSSN    DUP_Param    MoneyAmount  Active
888667000     2                    100.00                   9
888667000     2                    120.00                   9
888667000     2                    125.00                   1
888667000     0                    175.00                   0
787994098     2                    100.00                   9
787994098     2                    120.00                   1
555443000     1                    45.00                     0
555443000     2                    40.00                     1
474747474     2                    40.00                     1
474747474     2                    40.00                     9
Purchases.txt
Microsoft SQL ServerMicrosoft SQL Server 2005Microsoft SQL Server 2008

Avatar of undefined
Last Comment
PortletPaul

8/22/2022 - Mon