簡體   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