好的 - 我有一张带有索赔ID和程序代码组合的表 - 我需要做的是隔离只存在以8开头的程序代码的claimID - 所以在下面,我需要能够只获取那些带有claimIDs的记录,其中存在claimID LIKE'8%'代码 - 没有其他procedureCodes等 - 所以只有2和3来自下面的claimID
claimID procedureCode
1 85025
1 97110
2 85025
2 80102
3 87112
3 81020
如果有任何以不同数字开头的声明记录,我想排除声明ID。
答案 0 :(得分:2)
SELECT DISTINCT yt1.ClaimID
FROM YourTable yt1
WHERE yt1.procedureCode LIKE '8%'
AND NOT EXISTS (SELECT 1
FROM YourTable yt2
WHERE yt2.ClaimID = yt1.ClaimID
AND yt2.procedureCode NOT LIKE '8%');
答案 1 :(得分:1)
我们分三步完成。第一步是查找失败"没有其他前导数字"需求。第二步是找到满足的记录,必须以8"需求。第三步是将两个集合与exclusion join匹配,以便我们从步骤2中取出集合并从第1步中排除集合:
我们还需要知道procedureCode
列的数据类型。了解一点CPT codes,我认为char(5)
或类似是最合适的选择,具体取决于你如何处理修饰符。如果这是你存储它的方式,那么这应该有效:
With BadClaims As ( --Step 1
SELECT distinct ClaimID
FROM [Table]
WHERE procedureCode NOT LIKE '8%'
), GoodClaims AS ( --Step 2
SELECT distinct ClaimID
FROM [Table]
WHERE procedureCode LIKE '8%'
)
SELECT g.ClaimID --Step 3
FROM GoodClaims g
LEFT JOIN BadClaims b on b.ClaimID = g.ClaimID
WHERE b.ClaimID IS NULL
但是,您可能已将其存储为整数/数字字段。如果是这样的话,这应该有效:
With BadClaims As ( --Step 1
SELECT distinct ClaimID
FROM [Table]
WHERE procedureCode < 80000 or procedureCode >= 90000
), GoodClaims AS ( --Step 2
SELECT distinct ClaimID
FROM [Table]
WHERE procedureCode LIKE >= 80000 and procedureCode < 90000
)
SELECT g.ClaimID --Step 3
FROM GoodClaims g
LEFT JOIN BadClaims b on b.ClaimID = g.ClaimID
WHERE b.ClaimID IS NULL
这是一个很长的形式,用来解释这里发生了什么。您还可以将这些步骤简化为单个自联接:
SELECT distinct t1.claimID
FROM [Table] t1
LEFT JOIN [Table] t2 ON t2.claimID = t1.ClaimID and t2.procecureCode NOT LIKE '8%'
WHERE t1.procedureCode LIKE '8%' and t2.claimID IS NULL