0

I'm trying to retrieve a list of results where there are multiple rows which have a duplicate field in different rows but only want to retrieve the row which has been created most recently

Data

loc     |   created            |   dest    |   w   |   l   |   h          
--------------------------------------------------------------------
2       |   2020/11/09 07:00:00 |   north   |   12  |   10  |   34
3       |   2020/11/09 07:10:00 |   south   |   34  |   67  |   23
3       |   2020/11/09 08:13:00 |   west    |   67  |   22  |   12  

I have tried the the following which does give me the rows I require but is missing the extra columns which I require.

Select loc, MAX(created)
from Data
Group By loc

Results Required

loc     |   created            |   dest    |   w   |   l   |   h          
--------------------------------------------------------------------
2       |   2020/11/09 07:00:00 |   north   |   12  |   10  |   34
3       |   2020/11/09 08:13:00 |   west    |   67  |   22  |   12  
Phill360
  • 27
  • 3

2 Answers2

0
with max_vals as (
Select loc, MAX(created) as max_created
from Data
Group By loc
)
select d.*
from Data d join max_vals m
on d.loc = m.loc and d.created = m.max_created
drum
  • 5,416
  • 7
  • 57
  • 91
0

Try this:

select *
from (
    select *, row_number() over (partition by loc order by created desc) rn
    from Data
) t
where rn=1
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794