基于另一个字段的 Case Statement 结果

Case Statement results based on another field

我正在尝试创建一个 case 语句以仅根据 MortgageStatus 中的值显示 AmountRequested 的值 基本上,如果 MortgageStatus = 4 或 5 或 6 或 7 那么我想查看 AmountRequested 的值,否则显示 0

SELECT  s.ISDESC as Source,
p.ClientId,p.Id AS [Profile ID],c.CaseTypeDesc As [Case Type],u.FullName AS Advisor,
ISNULL(app.StatusDesc, ' No contact') as [Mortgage status] ,
CASE pm.AmountRequested
WHEN  pm.MortgageStatus IN (4,5,6,7) THEN pm.AmountRequested
Else '0'
END,
em.Description,
pm.ProcFeeExp,
pm.ArrangementFee,t.DueDate as [Enquiry Date],s.ISDESC
From Tasks AS t
LEFT JOIN dbo.tbl_Profile AS p
    ON t.FK_ProfileId = p.ID
LEFT JOIN dbo.tbl_CaseTypes AS c
    ON c.CaseTypeID = p.CaseTypeID
LEFT JOIN dbo.tbl_User AS u
    ON u.UserId = t.takenBy
LEFT JOIN tbl_Profile_Mortgage AS pm
    ON p.Id = pm.FK_ProfileId
LEFT JOIN tbl_AppStatus AS app
    ON pm.MortgageStatus = app.StatusId
LEFT JOIN tbl_DDEnquiryMethod AS em 
    ON t.EnquiryMethod = em.id
LEFT JOIN dbo.tbl_EnquiryType AS e 
    ON t.EnquiryType = e.EnquirytypeId
LEFT JOIN tbl_DDInitialSource as s
On t.EnquirySource = s.ISID

谢谢

我认为 CASE 语法不正确。试试这个:

      SELECT s.ISDESC as Source,
         p.ClientId,
         p.Id AS [ Profile ID ],
         c.CaseTypeDesc As [ Case Type ],
         u.FullName AS Advisor,
         ISNULL(app.StatusDesc, ' No contact') as [ Mortgage status ],
         CASE  WHEN pm.MortgageStatus IN (4, 5, 6, 7) THEN
            pm.AmountRequested
           Else
            '0'
         END,
         em.Description,
         pm.ProcFeeExp,
         pm.ArrangementFee,
         t.DueDate as [ Enquiry Date ],
         s.ISDESC
    From Tasks AS t
    LEFT JOIN dbo.tbl_Profile AS p
      ON t.FK_ProfileId = p.ID
    LEFT JOIN dbo.tbl_CaseTypes AS c
      ON c.CaseTypeID = p.CaseTypeID
    LEFT JOIN dbo.tbl_User AS u
      ON u.UserId = t.takenBy
    LEFT JOIN tbl_Profile_Mortgage AS pm
      ON p.Id = pm.FK_ProfileId
    LEFT JOIN tbl_AppStatus AS app
      ON pm.MortgageStatus = app.StatusId
    LEFT JOIN tbl_DDEnquiryMethod AS em
      ON t.EnquiryMethod = em.id
    LEFT JOIN dbo.tbl_EnquiryType AS e
      ON t.EnquiryType = e.EnquirytypeId
    LEFT JOIN tbl_DDInitialSource as s
      On t.EnquirySource = s.ISID