1

私は次の表を持っています

Location Type     Date
A        TestType 10-10-2013
A        TestType 05-05-2013
A        BestType 06-06-2013
B        TestType 09-09-2013
B        TestType 01-01-2013

タイプに関係なく、各場所の最大日付を返したいのですが、3 つの列すべてを返す必要があります。

望ましい結果:

Location Type     Date
A        TestType 10-10-2013
B        TestType 09-09-2013

これを行う最良の方法は何ですか?

の使用を検討しましたがRANK() Over Partition、適切に動作させることができません。

4

2 に答える 2

5

row_number()関数 を使用して、各場所partition by location ordering by [date] desc の を取得します。 max date

;with cte as (
   select location, type, [date], 
          row_number() over (partition by location order by [date] desc) rn
   from yourTable
)
select location, type, [date]
from cte
where rn = 1 --<<-- rn = 1 gets the max date for each location.

フィドルのデモ

于 2013-11-04T16:07:25.153 に答える