Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Update using SubQuery

Author  Topic 

Steve33056
Starting Member

2 Posts

Posted - 2010-02-23 : 11:39:18
Hello,
My name is Steve. This is a great resource and extremely valuable.

Here is what I am trying to accomplish.

I have a table with 3 subclaims.
Policy SubClaim cStatus cPureClaimStatus
CA-27979-0 CAFL0000000049 CL NULL
CA-27979-0 CAFL0000000049 OP NULL
CA-27979-0 CAFL0000000049 OP NULL

I want to update all cPureClaimStatus of each subclaim to 'OP' if any cStatus value is 'OP' else cStatus = 'CL'. So in the case above I should have cPureClaimStatus = 'OP' for all 3 records even though one of the subclaims is CL for closed.

End Result
Policy SubClaim cStatus cPureClaimStatus
CA-27979-0 CAFL0000000049 CL OP
CA-27979-0 CAFL0000000049 OP OP
CA-27979-0 CAFL0000000049 OP OP


How do I do this? Thanks in advance for your help.

Steve33056
Starting Member

2 Posts

Posted - 2010-02-23 : 11:41:13
Hello Again,

BTW, The subquery will be on the same table as these fields are in. There is no second table. The table name is tblMIS_CLM_ClaimInfo

Thanks again
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2010-02-23 : 12:12:55
[code]UPDATE t
SET t.cPureClaimStatus =CASE WHEN t1.StsCnt > 0 THEN 'OP' ELSE 'CL' END
FROM Table t
INNER JOIN (SELECT Policy, SubClaim, SUM(CASE WHEN cStatus='OP' THEN 1 ELSE 0 END) AS StsCnt
FROM table
GROUP BY Policy, SubClaim)t1
ON t1.Policy = t.Policy
AND t1.SubClaim = t.SubClaim
[/code]

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
   

- Advertisement -