繁体   English   中英

我想使用SELECT语句中其他列的值更新空列的值

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

我有一个带有多个联接的选择语句,我在SQL语句中也有一个空白列,我想用另一个也在同一选择语句中的列的值来更新此NULL列的值。

我尝试了更新语句,但是没有用。

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

我想替换第一列借贷组,以便如果状态为6或4,则为“ R”,否则为“ P”

将CASE与IN子句一起使用

 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

将其放在您要插入的列的代码中:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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