简体   繁体   中英

How can I do an Combined UPDATE statement and Multiple Different Condition in SQL?

I'm using SQL Server 2016 and trying to use SQL to update table at once with one query: Query has Different Where Condition and Sub query. I have Tried Case when Condition but not able to get desire result

Update TPort
 set DRecd = 
((select IsNull(Sum(RSrate), 0) from TDiv Divd 
Where TPort.SCCode = Divd.SchemeCode and
(CASE WHEN Divd.[RecordDate] > convert(varchar, TPort.TR_DATE, 101) and SCCode not in (3360,2059,291,288,2041,4003,5429,3724) THEN 1  
     WHEN Divd.[RecordDate] >= convert(varchar, TPort.TR_DATE, 101) and SCCode  in (4003,5429,3724) THEN 1 
     WHEN Divd.EXDIVDATE >= convert(varchar, TPort.TR_DATE, 101) AND  SCCode in (3360) THEN 1 
     WHEN Divd.EXDIVDATE > convert(varchar, TPort.TR_DATE, 101) AND SCCode in (2059,291,288,2041) THEN 1 
     ELSE 0
    END )=1
and Divd.[RecordDate] < CONVERT(varchar,GETDATE(),101)
) * UNITS) 
Where TType like 'Reinvestment%' and IType = '3' 

Here is 4 Query:

 Update TPort
     set DRecd = 
    ((select IsNull(Sum(RSrate), 0) from TDiv Divd 
    Where TPort.SCCode = Divd.Scode and Divd.[RecordDate] > convert(varchar, TPort.TR_DATE, 101)
    and Divd.[RecordDate] < CONVERT(varchar,GETDATE(),101)
    ) * UNITS) 
    Where TType like 'RInvest%' and IType = '3' and SCCode not in (3360,2059,291,288,2041,4003,5429,3724)


Update TPort set DRecd = ((select IsNull(Sum(RSrate), 0) from TDiv Divd 
Where TPort.SCCode = Divd.Scode and Divd.[RecordDate] >= convert(varchar, TPort.TR_DATE, 101)
and Divd.[RecordDate] < CONVERT(varchar,GETDATE(),101)
) * UNITS) 
Where TType like 'RInvest%' and IType = '3' and SCCode  in (4003,5429,3724)


Update TPort set DRecd = ((select IsNull(Sum(RSrate), 0) from TDiv Divd 
Where TPort.SCCode = Divd.Scode and Divd.EXDIVDATE >= convert(varchar, TPort.TR_DATE, 101)
and Divd.[RecordDate] < CONVERT(varchar,GETDATE(),101)
) * UNITS) 
Where TType like 'RInvest%' and IType = '3'  and SCCode in (3360)


Update TPort set DRecd = ((select IsNull(Sum(RSrate), 0) from TDiv Divd 
Where TPort.SCCode = Divd.Scode and Divd.EXDIVDATE > convert(varchar, TPort.TR_DATE, 101)
and Divd.[RecordDate] < CONVERT(varchar,GETDATE(),101)
) * UNITS) 
Where TType like 'RInvest%' and IType = '3'  and SCCode in (2059,291,288,2041)

But Problem is it's taking sum of Rsrate should be Different for each where for subquery?If you check All Four Query all are same except there SCcode and subquery condition . I hope that makes it clearer.

How can I do this in MS SQL Server? Your help is very much appreciated. Thank you!

You can try below syntax for your update query -

Update TP
SET DRecd = IsNull(Sum(RSrate), 0) * UNITS
FROM TPort TP
JOIN TDiv Divd ON TP.SCCode = Divd.SchemeCode
AND (CASE WHEN Divd.[RecordDate] > convert(varchar, TPort.TR_DATE, 101) AND SCCode not in (3360,2059,291,288,2041,4003,5429,3724) THEN 1  
          WHEN Divd.[RecordDate] >= convert(varchar, TPort.TR_DATE, 101) AND SCCode in (4003,5429,3724) THEN 1 
          WHEN Divd.EXDIVDATE >= convert(varchar, TPort.TR_DATE, 101) AND SCCode in (3360) THEN 1 
          WHEN Divd.EXDIVDATE > convert(varchar, TPort.TR_DATE, 101) AND SCCode in (2059,291,288,2041) THEN 1
          ELSE 0 END) = 1
AND Divd.[RecordDate] < CONVERT(varchar,GETDATE(),101)
WHERE TType like 'Reinvestment%' and IType = '3'

Ok Got Answer With Little Correction of Above Answer

UPDATE TPort 
SET DRecd =
((select IsNull(Sum(RSrate), 0) from TDiv Divd 
Where TPort.SCCode = Divd.SchemeCode 
AND (CASE WHEN Divd.[RecordDate] > convert(varchar, TPort .TR_DATE, 101) AND SCCode not in (3360,2059,291,288,2041,4003,5429,3724) THEN 1  
          WHEN Divd.[RecordDate] >= convert(varchar, TPort .TR_DATE, 101) AND SCCode  in (4003,5429,3724) THEN 1 
          WHEN Divd.EXDIVDATE >= convert(varchar, TPort .TR_DATE, 101) AND SCCode in (3360) THEN 1 
          WHEN Divd.EXDIVDATE > convert(varchar, TPort .TR_DATE, 101) AND SCCode in (2059,291,288,2041) THEN 1
          ELSE 0 END) = 1
and SCH_CODE = SCCode 
) * UNITS) 
Where Tran_Type in ('Reinvestment','ReinvestmentAm','ReInvestmentTI') and IType = '3' and SCH_CODE = OSch_Code 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM