如何删除SQL Server中的不完全重复项
目前,我可以从每个报告中获取数据,并按案例类型进行筛选,然后在案例打开时再次获取我想要的每个案例报告的数据 然而,作为一个案件可以在几个月内打开,我只希望它出现的第一个月。例如,一个案例可能在每个报告201904、201905中打开,然后在201911中重新打开,关于该案例的大量信息会发生变化,因此它不是一个精确的副本,但是我只关注201904报告中的案例数据 目前我正在使用以下代码如何删除SQL Server中的不完全重复项,sql,sql-server,tsql,date,greatest-n-per-group,Sql,Sql Server,Tsql,Date,Greatest N Per Group,目前,我可以从每个报告中获取数据,并按案例类型进行筛选,然后在案例打开时再次获取我想要的每个案例报告的数据 然而,作为一个案件可以在几个月内打开,我只希望它出现的第一个月。例如,一个案例可能在每个报告201904、201905中打开,然后在201911中重新打开,关于该案例的大量信息会发生变化,因此它不是一个精确的副本,但是我只关注201904报告中的案例数据 目前我正在使用以下代码 Select ReportDate, CaseNo, Est, CaseType From output.cas
Select ReportDate, CaseNo, Est, CaseType
From output.casedata
Where casetype='family' and Status='Open' AND (
Reportdate='201903' OR Reportdate='201904' OR Reportdate='201905'
or Reportdate='201906' or Reportdate='201907' or Reportdate='201908'
or Reportdate='201909' or Reportdate='201910' or Reportdate='201911'
or Reportdate='201912' or Reportdate='202001' or Reportdate='202002'
)
您可以使用rank window函数查找每个案例编号包含第一个日期的行,然后从中获取所有详细信息:
挑选*
从SELECT*,按案例在分区上排名按报告日期排序为rk
从output.casedata
其中casetype='family'和status='Open't
其中rk=1
您可以使用rank window函数查找每个案例编号包含第一个日期的行,然后从中获取所有详细信息:
挑选*
从SELECT*,按案例在分区上排名按报告日期排序为rk
从output.casedata
其中casetype='family'和status='Open't
其中rk=1
如果我没有弄错你的话,你想要每个案件最早的公开记录
以下内容应符合您的期望:
select c.*
from output.casedata c
where c.reportdate = (
select min(c1.reportdate)
where
c1.caseno = c.caseno
and c1.casetype = 'family'
and c1.status = 'open'
and c1.reportdate between '201903' and '202002'
)
对于性能,您需要一个关于caseno、casttype、status和reportdate的索引
请注意,我简化了reportdate上的筛选器以在之间使用,而不是枚举所有可能的值。如果我正确地遵循了您的操作,则您需要每个案例最早打开的记录
以下内容应符合您的期望:
select c.*
from output.casedata c
where c.reportdate = (
select min(c1.reportdate)
where
c1.caseno = c.caseno
and c1.casetype = 'family'
and c1.status = 'open'
and c1.reportdate between '201903' and '202002'
)
对于性能,您需要一个关于caseno、casttype、status和reportdate的索引
请注意,我简化了reportdate上的筛选器以使用between,而不是枚举所有可能的值。完全符合我的要求。谢谢完全符合我的要求。谢谢