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 に答える