案例陈述结果基于另一个字段

时间:2018-11-15 11:11:24

标签: sql tsql case case-statement

我试图创建一个case语句,仅根据AmountRequested中的值显示MortgageStatus的值 基本上,如果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

谢谢

1 个答案:

答案 0 :(得分:1)

我认为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