【问题标题】:Select latest record with some more conditions选择具有更多条件的最新记录
【发布时间】:2010-08-09 00:12:33
【问题描述】:
ID    Level Effective Date  ExpirationDate
000012-12     2 12/01/2005  NULL
000012-12    1    12/01/2005  NULL
000012-12    2    12/01/2005  01/01/2009
000012-A12    2 10/01/1994  11/30/2005
000012-A12    2 01/01/1999  11/30/2005
000012-A12    2 09/01/2001  11/30/2005
000012-A12    1 12/01/2005  12/31/2007

只会获取最新的记录。这意味着在上述情况下
过期日期 - 如果 null 记录仍然有效。
如果大于当前时间戳,则它的未来 exp date ,这意味着仍然有效。
如果小于当前时间戳,则终止。
大多数当前是最活跃或最近终止的记录。如果它已激活并已终止,则只会显示激活状态。其他最后终止的记录。

一个 ID 可以有 2 行相同的生效日期和到期日期,但有多个级别。因此,在这种情况下,我们只需要为一级选择 1 条记录。

所以根据上面的数据集,下面是预期的输出

输出

000012-12    1   12/01/2005  NULL
000012-A12  2   12/01/2005  01/01/2009

请帮忙

托马斯。请查看以下数据集。

Insert #Test( Id, Level, EffectiveDate, ExpirationDate ) Values ('000872-A24',1,'1994-10-01',NULL); 
Insert #Test( Id, Level, EffectiveDate, ExpirationDate ) Values ('000872-A24',1,'1999-01-01',NULL);  
Insert #Test( Id, Level, EffectiveDate, ExpirationDate ) Values ('000872-A24',2,'2001-09-01',NULL ); 
Insert #Test( Id, Level, EffectiveDate, ExpirationDate ) Values ('000872-A24',1,'2003-01-01','2007-12-31'); 

当你运行查询时它应该给出

000872-A24 2 09/01/2001 空

但现在它返回了

000872-A24 1 01/01/2003 12/31/2007

【问题讨论】:

  • @sqllearner190 - 表是否有主键,例如 auto_increment 或标识列?
  • @sqllearner190 - 您能否在您的示例数据中证明需要您的需求列表中第 2 项的第二个条件的情况?
  • 这就是我的想法。因为当他们要求最新记录时,它应该总是从一堆中给出 1 ..然后这不会对级别有任何依赖..但是我仍然如何选择最新的记录(基于到期日期)
  • @sqllearner190 - 我们需要更多信息来提供合理的答案。已经发布了一个回答,其中提出了其中一些问题。
  • Thomas,我刚刚得到了关于需求的澄清。下面是方案 ID 级别 生效日期 ExpirationDate 000012-12 2 12/01/2005 NULL 000012-12 1 12/01/2005 NULL 000012-12 2 12/01/2005 01/01/2009 000012-A12 2 10/01 /1994 11/30/2005 000012-A12 2 01/01/1999 11/30/2005 000012-A12 2 09/01/2001 11/30/2005 000012-A12 1 12/01/2005 12/31/2007 i需要选择过期日期的最新记录..如果为空,则选择该记录..如果我们有多个具有相同生效日期和到期日期的记录,则选择第一级。我必须为所有 Id 这样做

标签: sql sql-server-2008 record


【解决方案1】:

在不了解数据库产品的情况下很难提供答案。

1. if there is no auto_increment/identity column 
2. and if there is no other primary key (which is a bad idea obviously) 
3. and if the given database product supports `CURRENT_TIMESTAMP` (each DBMS will likely have some equivalent to the current date and time) 
4. and if the target date by which you measure "latest" is the current date and time


Select Id, Level
From Table As T
Where T. EffectiveDate =    (
                            Select Max(T2.EffectiveDate)
                            From Table As T2
                            Where T2.ID = T.ID
                                And ( T2.EffectiveDate Is Null
                                    Or (
                                        CURRENT_TIMESTAMP >= T2.EffectiveDate
                                        And CURRENT_TIMESTAMP <= T2.ExpirationDate
                                        )
                                    )
                            )   

您会在我的回答中注意到一些警告。这表明我们需要更多信息:

  1. 什么数据库产品和版本?
  2. 表上是否有自动递增的唯一键?
  3. 级别如何适应您想要的结果? (请扩展您的示例数据以包含极端情况)。
  4. 如果当前日期和时间早于具有无效到期日期的生效日期,会发生什么情况?

编辑

现在我们知道您使用的是 SQL Server 2008,这使得解决方案更容易:

If object_id('tempdb..#Test') is not null
    Drop Table #Test;
GO
Create Table #Test (
                    PkCol int not null identity(1,1) Primary Key
                    , Id varchar(50) not null
                    , Level int not null
                    , EffectiveDate datetime not null
                    , ExpirationDate datetime null
                    );

Insert #Test( Id, Level, EffectiveDate, ExpirationDate ) Values ('000012-12',2,'12/01/2005',NULL);
Insert #Test( Id, Level, EffectiveDate, ExpirationDate ) Values ('000012-12',1,'12/01/2005',NULL); 
Insert #Test( Id, Level, EffectiveDate, ExpirationDate ) Values ('000012-12',2,'12/01/2005','01/01/2009');
Insert #Test( Id, Level, EffectiveDate, ExpirationDate ) Values ('000012-A12',2,'10/01/1994','11/30/2005');
Insert #Test( Id, Level, EffectiveDate, ExpirationDate ) Values ('000012-A12',2,'01/01/1999','11/30/2005');
Insert #Test( Id, Level, EffectiveDate, ExpirationDate ) Values ('000012-A12',2,'09/01/2001','11/30/2005');
Insert #Test( Id, Level, EffectiveDate, ExpirationDate ) Values ('000012-A12',1,'12/01/2005','12/31/2007');

With Items As
    (
    Select PkCol, Id, Level, EffectiveDate, ExpirationDate
        , Row_Number() Over (   Partition By Id
                                Order By EffectiveDate Desc, Coalesce(ExpirationDate,'99991231') Desc, Level Asc ) As Num
    From #Test
    )
Select PkCol, Id, Level, EffectiveDate, ExpirationDate
From Items
Where Num = 1

在您的示例输出中,您的组合 ('000012-A12',2,'12/01/2005','01/01/2009') 没有出现在您的原始数据中。

我正在使用 SQL Server 2005 中添加的两个功能:公用表表达式和排名函数。公共表表达式Item 的作用类似于就地视图或查询。排名函数Row_Number 是真正神奇的地方。顾名思义,它返回由Order By 子句排序的数字的顺序列表。但是,它还会为每个 Id 值重新编号(即Partition By 位)。通过过滤Num = 1,我将返回每个 ID 的“顶部”值。

【讨论】:

  • Thomas,我刚刚得到了关于需求的澄清。下面是方案 ID 级别 生效日期 ExpirationDate 000012-12 2 12/01/2005 NULL 000012-12 1 12/01/2005 NULL 000012-12 2 12/01/2005 01/01/2009 000012-A12 2 10/01 /1994 11/30/2005 000012-A12 2 01/01/1999 11/30/2005 000012-A12 2 09/01/2001 11/30/2005 000012-A12 1 12/01/2005 12/31/2007 我需要选择过期日期的最新记录..如果为空,则选择该记录..如果我们有多个具有相同生效日期和到期日期的记录,则选择第一级。我必须为 tbl 中的所有 Id 这样做
  • @sqllearner190- 您应该使用修改后的示例数据更新您的原始帖子。其他问题(db 版本、auto_incrementing/identity 列、用于比较的目标日期)呢?
  • Thomas,我已经更新了要求。请帮助。该表也有标识列。
  • 哇...你做了什么!这似乎是非常高级的 SQL 编程.. :-) ..你能解释一下代码吗..
  • @Thomas - 我已经更新了我的问题。该逻辑不适用于我在那里提供的一组值。
猜你喜欢
  • 2021-08-23
  • 2023-03-16
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-02-06
相关资源
最近更新 更多