我有一个简单的TSQL查询:
SELECT dbo.tblHospEpisode.PATNT_PASID, [Pat_Given]+' '+[Pat_Family] AS Name, dbo.tblPhysiology.Pregnant, dbo.tblCritCareAdmit.CCDateAdmit, dbo.tblCritCareAdmit.CCDateDis, dbo.tblCritCareAdmit.CCOutcome
From dbo.tblPhysiology, dbo.tblCritCareAdmit, dbo.tblHospEpisode, dbo.tblDemographic
WHERE dbo.tblPhysiology.CC_ID=dbo.tblCritCareAdmit.CC_ID
AND dbo.tblCritCareAdmit.HSPEP_ID = dbo.tblHospEpisode.HSPEP_ID
AND dbo.tblHospEpisode.PATNT_PASID = dbo.tblDemographic.PATNT_PASID
AND dbo.tblPhysiology.Pregnant != 2
问题是某些字段返回需要映射到字符串的数字(数据库中没有映射。例如,怀孕代码1 =“当前怀孕”。我希望查询返回String而不是而不是代码。我不知道该怎么做?
答案 0 :(得分:1)
您想使用案例陈述:
SELECT he.PATNT_PASID, [Pat_Given]+' '+[Pat_Family] AS Name,
p.Pregnant, cca.CCDateAdmit, cca.CCDateDis, cca.CCOutcome,
(case when p.Pregnant = 1 then 'Currently Pregnant'
. . .
else '???'
end) as PregString
From dbo.tblPhysiology p
dbo.tblCritCareAdmit cca
on p.CC_ID = cca.CC_ID join
dbo.tblHospEpisode he
on cca.HSPEP_ID = he.HSPEP_ID join
dbo.tblDemographic d
on he.PATNT_PASID = d.PATNT_PASID
WHERE p.Pregnant <> 2;
我还为查询使用了表别名来简化查询,并将join
更改为使用on
子句的显式连接。
答案 1 :(得分:0)
您可以使用CASE表达式,如下所示:
select
case code
when 1 then 'Currently pregnant'
when 2 then 'Not Pregnant'
when 3 then 'Gave Birth'
end CodeDescription
from
YourTable