0

我在 Sybase ASE 中有以下数据

   id   effectiveDate     lastModificationDate  rateValue    
 -----  ----------------  --------------------  ------------ 
 1      20130627          6/27/2013 3:27:09 AM  0            
 1      20130627          6/27/2013 4:39:10 AM  2.75         
 1      20130627          6/28/2013 3:48:15 AM  0            
 1      20130627          6/28/2013 4:36:43 AM  2.75         
 1      20130628          6/28/2013 3:48:14 AM  0            
 1      20130628          6/28/2013 4:36:42 AM  2.75         
 2      20130628          6/28/2013 4:36:42 AM  .75         
 2      20130628          6/28/2013 3:48:14 AM  0            

我如何对它进行分组,以便我只得到最后一行,即我得到具有相同 id+effectiveDate 的最大 lastModificationDate 的行。

所以输出将是:

 id     effectiveDate     lastModificationDate  value    
 -----  ----------------  --------------------  ------------ 
 1      20130627          6/28/2013 4:36:43 AM  2.75         
 1      20130628          6/28/2013 4:36:42 AM  2.75         
 2      20130628          6/28/2013 4:36:42 AM  .75         

请注意,这将在 TSQL (Sybase ASE 15) 上。编辑:已更改数据以使其更真实

4

2 回答 2

0

尝试:

SELECT t1.*
FROM Table1 t1
WHERE t1.lastModificationDate  = (SELECT MAX(t2.lastModificationDate)
                                  FROM Table1 t2
                                  WHERE t2.effectiveDate = t1.effectiveDate
                                  AND t2.id = t1.id)

Sybase 文档:

子查询可以嵌套在外部 select、insert、update 或 delete 语句的 where 或 having 子句中、另一个子查询中或选择列表中。或者,您可以编写许多包含子查询作为连接的语句;Adaptive Server 将诸如连接之类的语句处理。

于 2013-07-01T07:11:32.320 回答
0

避免使用子查询的另一个答案是......

select id, effectiveDate, lastModificationDate, rateValue 
from #mydata
group by id, effectiveDate
having lastModificationDate = max(lastModificationDate)

如果我认为您的数据存储在 #mydata 临时表中

create table #mydata(
    id                   int      null,
    effectiveDate        char(8)  null,
    lastModificationDate datetime null,
    rateValue            money    null
)

insert into #mydata(id, effectiveDate, lastModificationDate, rateValue) select 1, "20130627", "6/27/2013 3:27:09 AM", 0            
insert into #mydata(id, effectiveDate, lastModificationDate, rateValue) select 1, "20130627", "6/27/2013 4:39:10 AM", 2.75         
insert into #mydata(id, effectiveDate, lastModificationDate, rateValue) select 1, "20130627", "6/28/2013 3:48:15 AM", 0            
insert into #mydata(id, effectiveDate, lastModificationDate, rateValue) select 1, "20130627", "6/28/2013 4:36:43 AM", 2.75         
insert into #mydata(id, effectiveDate, lastModificationDate, rateValue) select 1, "20130628", "6/28/2013 3:48:14 AM", 0            
insert into #mydata(id, effectiveDate, lastModificationDate, rateValue) select 1, "20130628", "6/28/2013 4:36:42 AM", 2.75         
insert into #mydata(id, effectiveDate, lastModificationDate, rateValue) select 2, "20130628", "6/28/2013 4:36:42 AM", .75         
insert into #mydata(id, effectiveDate, lastModificationDate, rateValue) select 2, "20130628", "6/28/2013 3:48:14 AM", 0            
于 2013-08-07T15:47:15.130 回答