如何根据单个列值有条件地限制结果

时间:2016-10-13 14:30:55

标签: sql sql-server sql-server-2012

我正在使用第三方数据库,它大量使用行版本控制。通常这不是问题,但是对于特定的数据集,这是由于前端的业务规则松散。正如您将注意到的,INNER JOIN用于获取每个EffectiveDate最新EmpID的行组。除非有EnrollmentStatus = 'Enrolled'行,否则应始终使用此逻辑。如果存在,则应返回此组行。按行组,我的意思是EmpIDEffectiveDate

在下面的数据集中,期望的结果将是EffectiveDate = '2015-12-15'的4行,因为它包含EnrollmentStatus = 'Enrolled'的记录。如果每个EmpID的记录都没有EnrollmentStatus = 'Enrolled',则内部联接就足够了。

我确信我忽略了这样做的优雅方式。

if object_id('tempdb..#emp') is not null drop table #emp

create table #emp
    (EmpID int, 
    EmpBenID int,
    EffectiveDate datetime,
    EligibilityDate datetime,
    EnrollBeginDate datetime,
    OverrideEnrollBeginDate datetime,
    EnrollEndDate datetime,
    OverrrideEnrollEndDate datetime,
    EnrollStatus varchar(64))

insert into #emp(EmpID, EmpBenID, EffectiveDate, EligibilityDate, EnrollBeginDate,OverrideEnrollBeginDate,EnrollEndDate,OverrrideEnrollEndDate,EnrollStatus) 
values
(1950,55403,'2015-12-15 00:00:00','1998-11-02 00:00:00',NULL,NULL,NULL,NULL,'Not Enrolled'),
(1950,55404,'2015-12-15 00:00:00','1998-11-02 00:00:00','1998-12-01 00:00:00',NULL,NULL,NULL,'Enrolled'),
(1950,55405,'2015-12-15 00:00:00','1998-11-02 00:00:00',NULL,NULL,NULL,NULL,'Not Enrolled'),
(1950,55406,'2015-12-15 00:00:00','1998-11-02 00:00:00',NULL,NULL,NULL,NULL,'Not Enrolled'),
(1950,55407,'2016-01-12 00:00:00','1998-11-02 00:00:00',NULL,NULL,NULL,NULL,'Not Enrolled'),
(1950,55408,'2016-01-12 00:00:00','1998-11-02 00:00:00','2011-01-19 00:00:00',NULL,'2011-08-31 00:00:00',NULL,'Not Enrolled'),
(1950,55409,'2016-01-12 00:00:00','1998-11-02 00:00:00',NULL,NULL,NULL,NULL,'Not Enrolled'),
(1950,55410,'2016-01-12 00:00:00','1998-11-02 00:00:00',NULL,NULL,NULL,NULL,'Not Enrolled')


select e.* 
from #emp e
inner join
(select EmpID, Max(EffectiveDate) dt
from #emp
--Attempted multiple filtering methods here while trying to avoid a sub-select
group by EmpID) e2 on e2.EmpID = e.EmpID and e2.dt = e.EffectiveDate

2 个答案:

答案 0 :(得分:3)

我会使用窗口函数来做到这一点,但这是另一回事。如果我理解正确,你需要这个逻辑:

df = df[['noEvents', 'upTime']].astype(int)
df.index.name = 'IP'
df.columns.name = None
df

这将获取select e.* from #emp e inner join (select EmpID, coalesce(max(case when EnrollmentStatus = 'Enrolled' then EffectiveDate end), max(EffectiveDate) ) dt from #emp --Attempted multiple filtering methods here while trying to avoid a sub-select group by EmpID ) e2 on e2.EmpID = e.EmpID and e2.dt = e.EffectiveDate; 的日期(如果存在)。否则,它会获得最大日期。

答案 1 :(得分:1)

我会用这个:

select EmpID, EffectiveDate
from #emp e1
where EnrollStatus = 'Enrolled'
union
select e1.EmpID, max(e1.EffectiveDate)
from #emp e1
where e1.EnrollStatus <> 'Enrolled'
and not exists (select 1 from #emp e2 where e1.EmpID = e2.EmpID and e2.EnrollStatus = 'Enrolled')
group by e1.EmpID

它会获得注册者的生效日期,以及未注册者的最长生效日期(以及任何时候没有注册状态的人)