Reputation: 3118
I have a table looks like
ID, name, Likes, Login_time
select * from mytbl
I want to filter this table:
distinct ID, name like, login_time(last login time)
I tried this query, but it didn't work.
select *
from
(select
name, likes, login_time
rank() over (partition by id order by login_time desc) as rank
from
mytbl) t
where
t.rank = 1
Upvotes: 1
Views: 704
Reputation: 407
use row_number instead of rank
select *
from
(
select
id, name, likes, login_time,
ROW_NUMBER() over (partition by id order by login_time desc) as rank
from
mytbl )t
where
t.rank = 1
Upvotes: 1
Reputation: 5148
Try this
;WITH temps AS
(
SELECT Id, name, likes, login_time, row_number() over(PARTITION BY Id ORDER BY login_time desc) AS RowIndex
)
SELECT Id, name, likes, login_time FROM temps
WHERE RowIndex = 1
Upvotes: 0