create table #productcov
(
[group] varchar(20), -- as business
account varchar(10), -- as business location
member varchar(10), -- as member
product varchar(10), -- as health product
[plan] varchar(10), -- as product type
StartTime datetime, -- as beginning of coverage period
EndTime datetime -- as end of coverage period
)
go
insert into #productcov values
('NN00001','10001','1234567890','ADD','A1000','2010-10-01 00:00:00' , '2012-09-30 00:00:00'),
('NN00001','10001','1234567890','ADD','A1000','2012-10-01 00:00:00' , '2013-09-30 00:00:00'),
('NN00001','10001','1234567890','ADD','A1000','2013-01-01 00:00:00' , '2013-12-31 00:00:00'),
('NN00001','10001','1234567890','ADD','B1000','2010-01-01 00:00:00' , '2014-12-31 00:00:00'),
('NN00001','10001','1234567890','ADD','B1000','2015-01-01 00:00:00' , '2016-12-31 00:00:00'),
('NN00001','10001','1234567890','LIF','A1000','2010-10-01 00:00:00' , '2012-09-30 00:00:00'),
('NN00001','10001','1234567890','LIF','A1000','2012-10-01 00:00:00' , '2013-09-30 00:00:00'),
('NN00001','10001','1234567890','LIF','A1000','2013-01-01 00:00:00' , '2013-12-31 00:00:00'),
('NN00001','10001','1234567890','LIF','B1000','2010-01-01 00:00:00' , '2014-12-31 00:00:00'),
('NN00001','10001','1234567890','LIF','B1000','2015-01-01 00:00:00' , '2016-12-31 00:00:00')
select * from #productcov
上面的示例表适用于单个组中的单个成员。我试图从#productcov表中的十个记录中选择第二个,第三个,第七个和第八个记录,其中医疗产品的覆盖日期对于会员的产品是重叠的。这是一个清理覆盖日期中重叠数据的审计过程。表中重要的重要日期需要按组,帐户,成员,产品和计划进行分组。
我几天都在努力寻找解决方案,而且我一直在空白!任何帮助表示赞赏。
所需的输出将是下面列出的4条记录:
集团,帐户,会员,产品,计划,StartDate,EndDate
' NN00001'' 10001'' 1234567890'' ADD'' A1000'&# 39; 2012-10-01 00:00:00' ,' 2013-09-30 00:00:00',
' NN00001'' 10001'' 1234567890'' ADD'' A1000'&# 39; 2013-01-01 00:00:00' ,' 2013-12-31 00:00:00'
' NN00001'' 10001'' 1234567890'' LIF'' A1000'&# 39; 2012-10-01 00:00:00' ,' 2013-09-30 00:00:00'
' NN00001'' 10001'' 1234567890'' LIF'' A1000'&# 39; 2013-01-01 00:00:00' ,' 2013-12-31 00:00:00'
答案 0 :(得分:0)
您可以使用exists
:
select pc.*
from #productcov pc
where exists (select 1
from #productcov pc2
where pc2.[group] = pc.[group] and -- don't know if this is needed
pc2.member = pc.member and
pc2.product = pc.product and
pc.endtime > pc2.starttime and pc.starttime < pc2.endtime
) ;
注意:您可能需要根据具体的重叠规则将>
调整为>=
和<
调整为<=
。
答案 1 :(得分:0)
我终于能够自己为我们的生产数据库找出这个审核了!我在一个大型查询中使用了多个Common Table Expressions,Dense_Ranks,Row_Numbers来解决!稍后会发布测试数据查询的结果。
with cte as (
select distinct
DENSE_RANK() OVER (ORDER BY CONCAT(rtrim([group]),rtrim (account),rtrim(member), rtrim(product), rtrim([plan]))) AS [Key Count],
ROW_NUMBER() OVER (PARTITION BY CONCAT(rtrim([group]),rtrim(account), rtrim(member), rtrim(product), rtrim([plan])) order by CONCAT(rtrim([group]),rtrim(account), rtrim(member), rtrim(product), rtrim([plan])), StartTime) as [Overlap],
CONCAT(DENSE_RANK() OVER (ORDER BY CONCAT(rtrim([group]),rtrim(account), rtrim(member), rtrim(product), rtrim([plan]))), ROW_NUMBER() OVER (PARTITION BY CONCAT(rtrim([group]),rtrim(account), rtrim(member), rtrim(product), rtrim([plan])) order by CONCAT(rtrim([group]),rtrim(account), rtrim(member), rtrim(product), rtrim([plan])), StartTime)) as Identifier,
CONCAT(rtrim([group]),rtrim(account),rtrim(member), rtrim(product), rtrim([plan])) as [Key],
StartTime as [Eff Date],
case when EndTime = 0 then '29991231' else EndTime end as [Term Date],
[group],
account,
member,
product,
[plan]
from #productcov
where [group] like 'N%'),
cte2 as (
select distinct
DENSE_RANK() OVER (ORDER BY CONCAT(rtrim([group]),rtrim(account),rtrim(member), rtrim(product), rtrim([plan]))) AS [Key Count],
ROW_NUMBER() OVER (PARTITION BY CONCAT(rtrim([group]),rtrim(account), rtrim(member), rtrim(product), rtrim([plan])) order by CONCAT(rtrim([group]),rtrim(account), rtrim(member), rtrim(product), rtrim([plan])), StartTime) as [Overlap],
CONCAT(DENSE_RANK() OVER (ORDER BY CONCAT(rtrim([group]),rtrim(account), rtrim(member), rtrim(product), rtrim([plan]))), ROW_NUMBER() OVER (PARTITION BY CONCAT(rtrim([group]),rtrim(account), rtrim(member), rtrim(product), rtrim([plan])) order by CONCAT(rtrim([group]),rtrim(account), rtrim(member), rtrim(product), rtrim([plan])), StartTime)) as Identifier,
CONCAT(rtrim([group]),rtrim(account),rtrim(member), rtrim(product), rtrim([plan])) as [Key],
StartTime as [Eff Date],
case when EndTime = 0 then '29991231' else EndTime end as [Term Date],
[group],
account,
member,
product,
[plan]
from #productcov
where [group] like 'N%')
select distinct
cte2.[Group],
cte2.[Account],
cte2.[Member],
cte2.[Product],
cte2.[Plan]
from cte
left outer join cte2 on cte.[Key] = cte2.[Key]
where (cte2.[Eff Date] between cte.[Eff Date] and cte.[Term Date]
or cte2.[Term Date] between cte.[Eff Date] and cte.[Term Date])
and cte.[Key Count] = cte2.[Key Count]
and cte.[Identifier] <> cte2.[Identifier]
group by cte2.[Group], cte2.[Account], cte2.[Member], cte2.[Product], cte2.[Plan]
order by cte2.[Group], cte2.[Account], cte2.[Member], cte2.[Product], cte2.[Plan];