我想提出一个案例,我想检查这样的条件
如果列DischargeDispositionConversion的值被释放到家庭/自我护理(例行费用)。然后我需要将其更改为“Discharged to Home
”,但即使其价值为“出院到家庭/自我照顾”,它也应该更改为“Discharged to Home
”。
任何人都可以告诉我如何使这个查询更小?我知道这可以通过使用两个案例来实现,一个是括号,另一个是没有括号,但是还有其他任何方式可以使查询更小。
答案 0 :(得分:0)
听起来你有这个:
UPDATE MyData
SET
DischargeDispositionConversion = 'Discharged to Home'
WHERE
DischargeDispositionConversion = 'Discharged to home/self care (routine charge)'
OR 'Discharged to home/self care'
但是,您不想单独测试这些案例。坦率地说,这样做并不是坏事,如果只有两个有效条件,那么具体测试每个条件可能是完成它的最好方法。尽管如此,如果你的心脏开始缩短SQL,那么你可以这样做:
UPDATE MyData
SET
DischargeDispositionConversion = 'Discharged to Home'
WHERE
DischargeDispositionConversion LIKE 'Discharged to home/self care%'
请注意,这会带来各种陷阱。任何以“出院回家/自我照顾”为开头的事情。将被改为“出院回家”。假设该领域已经出院回家/自我照顾(针对医生的建议)'第二个命令最终将摧毁重要的信息。
答案 1 :(得分:0)
我不明白为什么你提出一个空间,但因为这两个值是
然后一个案子就可以了。
select
case
when DischargeDispositionConversion like 'Discharged to home/self care%'
then 'Discharged to Home'
else DischargeDispositionConversion
end
from
YourTable
答案 2 :(得分:0)
如果您在SSIS派生列转换中编写代码,则可以将其编写为
FINDSTRING([DischargeDispositionConversion], "Discharged to home/self care", 1) == 1 ? "Discharged to Home" : [DischargeDispositionConversion]
答案 3 :(得分:0)
鉴于你的评论有多达30或35个案例,我建议不要使用SQL来做出这个决定。您应该创建一个解码表并加入它。
这是一种高度灵活且易于维护的解决方案。如果您想为其他代码类型创建映射,它还将扩展到“Discharged to Home”之外的情况。最后,如果您需要更改值,它就像数据更新一样简单。无需重新部署。
create table DischargeDisposition_Decode (tableCode varchar(50), revisedCode varchar(50))
insert into DischargeDisposition_Decode(tableCode, revisedCode) VALUES
( 'Discharged to home/self care (routine charge)', 'Discharged to Home' )
, ( 'Discharged to home/self care', 'Discharged to Home')
, ( 'Some other discharged to home code_1', 'Discharged to Home')
, ( 'Some other discharged to home code_2', 'Discharged to Home')
, ( 'Discharged to somewhere that is NOT home', 'Somewhere that is not home')
.
.
.
Select
isNull(ddd.revisedCode, MyData.DischargeDispositionConversion)
from
MyData
LEFT JOIN DischargeDisposition_Decode ddd
ON myData.DischargeDispositionConversion = ddd.tableCode