0

3列のテーブルがありますObjectID, ChangedOn, NewValue

サンプルデータ:

ObjectId                                NewValue            ChangedOn
50E37991-777C-4813-B436-C6082CCA6BE2    Closed              2012-07-13 15:14:35.000
50E37991-777C-4813-B436-C6082CCA6BE2    ActiveForSupport    2012-07-13 15:13:33.930
50E37991-777C-4813-B436-C6082CCA6BE2    Closed              2012-07-13 15:12:47.787
50E37991-777C-4813-B436-C6082CCA6BE2    ActiveForSupport    2012-07-13 15:11:28.540
50E37991-777C-4813-B436-C6082CCA6BE2    Closed              2012-07-13 14:52:10.797
D9001738-E089-43B3-8E80-6993601C4E70    Closed              2012-06-27 08:03:27.550

グループデータが必要でObjectId、3つの列を表示します。グループへの最大値と現在のグループへの最大ObjectId, NewValue値を含む行の 場合テストデータの場合、次の結果を取得する必要があります。ChangedOnChangedOn


ObjectId NewValue ChangedOn
     50E37991-777C-4813-B436-C6082CCA6BE2クローズ2012-07-1315:14:35.000
     D9001738-E089-43B3-8E80-6993601C4E70クローズ2012-06-2708:03:27.550
4

1 に答える 1

0

これを試して

CREATE TABLE test3(ObjectId uniqueidentifier,NewValue varchar(100),ChangedOn datetime)
INSERT INTO test3
VALUES('50E37991-777C-4813-B436-C6082CCA6BE2','Closed','2012-07-13 15:14:35.000'),
('50E37991-777C-4813-B436-C6082CCA6BE2','ActiveForSupport','2012-07-13 15:13:33.930'),
('50E37991-777C-4813-B436-C6082CCA6BE2','Closed','2012-07-13 15:12:47.787'),
('50E37991-777C-4813-B436-C6082CCA6BE2','ActiveForSupport','2012-07-13 15:11:28.540'),
('50E37991-777C-4813-B436-C6082CCA6BE2','Closed','2012-07-13 14:52:10.797'),
('D9001738-E089-43B3-8E80-6993601C4E70','Closed','2012-06-27 08:03:27.550')

select t1.ObjectId,t1.NewValue,t2.maxChangedOn  from test3 t1 inner join 
(select ObjectId,MAX(ChangedOn) maxChangedOn from test3
group by ObjectId) t2
on t1.ObjectId =t2.ObjectId and t1.ChangedOn =t2.maxChangedOn 
于 2012-07-31T16:11:17.713 に答える