sql - 情况下 min()
问题描述
协助 CASE 语句的第二个 WHEN。我需要为以下场景编写代码:如果有两条 PCC = NULL 的记录,则选择具有最低值的 ProviderFinalTier。它目前正在选择表的 min providerfinaltier。
<!-- language: lang-sql-->
UPDATE spc
SET ProviderTier = CASE WHEN (coalesce(spc.rulespecialtyid, spc.RenderingproviderspecialtyID) = mft.SpecialtyID and coalesce(spc.RulePCC,spc.PCC) = mft.PCC ) THEN ProviderFinalTier
WHEN (coalesce(spc.rulespecialtyid, spc.RenderingproviderspecialtyID) = mft.SpecialtyID and mft.pcc is null)
THEN (Select min(ProviderFinalTier) from Common.medicarefinaltiers where coalesce(spc.rulespecialtyid, spc.RenderingproviderspecialtyID) = mft.SpecialtyID and mft.pcc is null)
ELSE 2 END
from analysis.AnalysisMatchedClaims spc
left join Compass_REPORTING.dbo.Payer p on p.Payer_Key = spc.PayerKey
left join Common.medicarefinaltiers mft on /*mft.EmployerId = spc.EmployerId
and */mft.ProviderID = spc.RenderingProviderId
and mft.SpecialtyId = coalesce(spc.rulespecialtyid, spc.RenderingproviderspecialtyID)
and mft.PayerGroup = p.Payer_Group
WHERE spc.ProviderTier is null
and (mft.pcc = coalesce(spc.RulePCC,spc.PCC) or mft.PCC is null)
and spc.RuleClaimType = 'I'
解决方案
在这个子查询中:
Select min(ProviderFinalTier)
from Common.medicarefinaltiers
where coalesce(spc.rulespecialtyid, spc.RenderingproviderspecialtyID) = mft.SpecialtyID
and mft.pcc is null
您没有为该表设置别名,Common.medicarefinaltiers
因此对外部连接表的任何引用mft
实际上都是对外部连接表的引用Common.medicarefinaltiers
。
我认为您应该为表提供别名并将该别名用于条件:
Select min(m.ProviderFinalTier)
from Common.medicarefinaltiers m
where coalesce(spc.rulespecialtyid, spc.RenderingproviderspecialtyID) = m.SpecialtyID
and m.pcc is null
推荐阅读
- c++ - Opencv - 如何获取图像中存在的垂直线数(行数)
- r - R字符串填充时间
- python - 访问不同目录中的 CSV 文件(Python)
- android - 当它添加了一个额外的 LinearLayout 时,RecyclerView 不起作用
- javascript - 从 Webpack 生成的微前端中找不到 React
- mysql - 我不小心更改了我的root权限,如何解决?
- flutter - 如何从flutter中的特定节点firebase实时数据库获取列表数据
- python - 通过它在 selenium 中的文本单击一个按钮
- c# - PC 重新启动后,Microsoft OPOS 挂在 Claim 上
- java - 我把我的try catch放在一个do while循环中,但是do-while循环之后的命令继续运行,即使有一个异常被捕获