我试图创建一个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
谢谢
答案 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