简体   繁体   中英

I want to update values of a null column using the values of other column in SELECT statement

I have a select statement with multiple joins I also have a blank column in SQL statement I want to update the value of this NULL column which I created with the values of another column which is also in the same select statement.

I tried update statement but it did not work.

SELECT
null as loan_group
,[Loan Number] as loan_number
,[DateofNote] as open_data
,[lnamount] as original_note_amount
,[EOM Balance] as current_principal_balance
,b.status


FROM 
    tblbordata2 B
    INNER JOIN tblLOANDATA2 L ON B.[Student ID] = L.[Student ID]
    INNER JOIN tblLoanStatus LS ON LS.[Status ID] = B.Status
    INNER JOIN SCHOOLS S ON S.SchoolNumber = b.SchoolNumber
    INNER JOIN [School Types] ST ON ST.[School Type ID] = S.SchoolID

loan_group  loan_number open_data   original_note_amount    current_principal_balance   status
NULL    1566    1997-10-14 00:00:00.000 6495.00 0.00    6
NULL    1564    1997-10-13 00:00:00.000 6495.00 0.00    4
NULL    1577    1997-10-17 00:00:00.000 2895.00 0.00    9
NULL    1557    1997-10-11 00:00:00.000 2875.00 0.00    6
NULL    1558    1997-10-14 00:00:00.000 3845.00 0.00    19
NULL    1561    1997-10-10 00:00:00.000 1995.00 0.00    19
NULL    1553    1997-10-13 00:00:00.000 3500.00 0.00    6
NULL    1548    1997-10-09 00:00:00.000 3645.00 0.00    19
NULL    1555    1997-10-13 00:00:00.000 2000.00 0.00    4
NULL    1582    1997-10-23 00:00:00.000 4500.00 0.00    19
NULL    1575    1997-10-18 00:00:00.000 3945.00 0.00    4
NULL    1574    1997-10-16 00:00:00.000 4600.00 0.00    19
NULL    1560    1997-10-15 00:00:00.000 3736.00 0.00    4
NULL    1594    1997-10-20 00:00:00.000 2500.00 0.00    6
NULL    1593    1997-10-28 00:00:00.000 3000.00 0.00    4
NULL    1591    1997-10-24 00:00:00.000 3862.00 0.00    6
NULL    1590    1997-10-23 00:00:00.000 6495.00 0.00    6
NULL    1586    1997-10-13 00:00:00.000 3395.00 0.00    6
NULL    1588    1997-10-18 00:00:00.000 3495.00 0.00    4
NULL    1587    1997-10-18 00:00:00.000 3495.00 0.00    6

I want to replace the first column loan group such that if the status is 6 or 4 "R" else "P"

Use CASE with IN clause

 SELECT

    case when status IN 
       ('6' , '4' )
        then 'R'
    else 'P'
  end as loan_group
,[Loan Number] as loan_number
,[DateofNote] as open_data
,[lnamount] as original_note_amount
,[EOM Balance] as current_principal_balance
,b.status

 FROM 
tblbordata2 B
INNER JOIN tblLOANDATA2 L ON B. 
 [Student ID] = L.[Student ID]
INNER JOIN tblLoanStatus LS ON LS. 
[Status ID] = B.Status
INNER JOIN SCHOOLS S ON 
 S.SchoolNumber = b.SchoolNumber
INNER JOIN [School Types] ST ON ST. 
[School Type ID] = S.SchoolID

Put this in the code for the column you are trying to insert:

SELECT
 case when loan_group is null then
        case when status = 6 or status = 4 then 'R'
        else 'P'
      end
    end
,[Loan Number] as loan_number
,[DateofNote] as open_data
,[lnamount] as original_note_amount
,[EOM Balance] as current_principal_balance
,b.status

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