这无疑是残暴的代码。我希望不仅能让它发挥作用,而且能让它运转良好。我想为每个max date
获取Claim Adjustment Type Code
。
当前代码:
SELECT a.[Current ClaimID], a.[Claim Adjustment Type Code], a.[Claim Effective Date]
FROM #tmp_hic_dupes_list_final_not10 a
join (select a.[Current ClaimID], a.[Claim Adjustment Type Code], a.[Claim Effective Date]
from #tmp_hic_dupes_list_final_not10 as a
where
a.[Claim Adjustment Type Code] in (SELECT a.[Claim Adjustment Type Code]
FROM
#tmp_hic_dupes_list_final_not10 b where
a.[Current HIC #] = b.[Current HIC #] and
a.[Claim Type Code] = b.[Claim Type Code] and
a.[Provider Oscar #] = b.[Provider Oscar #] and
a.[Claim From Date] = b.[Claim From Date] and
a.[Claim Thru Date] = b.[Claim Thru Date] and
a.[Claim Adjustment Type Code] = b.[Claim Adjustment Type Code]
HAVING
COUNT(*) > 1)) b on a.[Current ClaimID] = b.[Current ClaimID]
--WHERE a.[Claim Effective Date] < b.[Claim Effective Date]
group by a.[Current ClaimID], a.[Claim Adjustment Type Code], a.[Claim Effective Date]
having a.[Claim Effective Date] = max(a.[Claim Effective Date])
目前的重播:
[Current ClaimID] [Claim Adjustment Type Code] [Claim Effective Date]
37274993770 1 2014-02-07 00:00:00.000
37274993771 2 2014-02-07 00:00:00.000
37509451954 1 2014-02-21 00:00:00.000
37509451955 2 2014-02-21 00:00:00.000
38168035124 1 2014-04-04 00:00:00.000
38168035125 2 2014-04-04 00:00:00.000
预期结果:
[Current ClaimID] [Claim Adjustment Type Code] [Claim Effective Date]
38168035124 1 2014-04-04 00:00:00.000
38168035125 2 2014-04-04 00:00:00.000
答案 0 :(得分:1)
执行此操作的一种简单方法是添加一个where子句,将结果限制为那些不存在任何具有相同声明调整类型代码的行以及后续索赔生效日期。
尝试将此添加到您的查询中:
WHERE NOT EXISTS (
SELECT 1
FROM #tmp_hic_dupes_list_final_not10
WHERE [Claim Adjustment Type Code] = a.[Claim Adjustment Type Code]
AND [Claim Effective Date] > a.[Claim Effective Date]
)
此外,你可能可以摆脱最后一个条款,因为它似乎没有做任何事情(但没有测试数据,我并不是100%肯定)。
答案 1 :(得分:1)
您可以通过降序日期将RANK()函数添加到现有代码,RANK()每组类型代码,然后在外部查询中,仅选择rank = 1的记录
SELECT [Current ClaimID], [Claim Adjustment Type Code], [Claim Effective Date] FROM (
SELECT a.[Current ClaimID], a.[Claim Adjustment Type Code], a.[Claim Effective Date],
RANK() OVER (PARTITION BY a.[Claim Adjustment Type Code] ORDER BY a.[Claim Effective Date] DESC) AS RankClaimTypeByDate
FROM #tmp_hic_dupes_list_final_not10 a
join (select a.[Current ClaimID], a.[Claim Adjustment Type Code], a.[Claim Effective Date]
from #tmp_hic_dupes_list_final_not10 as a
where
a.[Claim Adjustment Type Code] in (SELECT a.[Claim Adjustment Type Code]
FROM
#tmp_hic_dupes_list_final_not10 b where
a.[Current HIC #] = b.[Current HIC #] and
a.[Claim Type Code] = b.[Claim Type Code] and
a.[Provider Oscar #] = b.[Provider Oscar #] and
a.[Claim From Date] = b.[Claim From Date] and
a.[Claim Thru Date] = b.[Claim Thru Date] and
a.[Claim Adjustment Type Code] = b.[Claim Adjustment Type Code]
HAVING
COUNT(*) > 1)) b on a.[Current ClaimID] = b.[Current ClaimID]
--WHERE a.[Claim Effective Date] < b.[Claim Effective Date]
group by a.[Current ClaimID], a.[Claim Adjustment Type Code], a.[Claim Effective Date]
having a.[Claim Effective Date] = max(a.[Claim Effective Date]) ) d WHERE RankClaimTypeByDate = 1 -- Select the 1st ranked record within each [Claim Adjustment Type Code]
答案 2 :(得分:1)
一种可能性是加入您选择的相同数据的子集,只抓取每个MAX([Claim Effective Date])
的{{1}}。这是对现有查询结果的一个额外步骤,而不是它的一部分(尽管这是你的选择):
[Claim Adjustment Type Code]