我得到了以下表格内容:
> id text time
> 1 Hi 2
> 2 Hello 1
> 1 Mr. SP 3
> 3 KK 1
> 2 TT 2
> 1 Sample 1
> 1 App 4
我需要为带有文本的id选择不同的值,因此输出应为:
id text time 1 App 4 2 TT 2 3 KK 1
任何人都可以帮我解决这个问题。
我正在使用Sqlite
由于
答案 0 :(得分:1)
select b.*
from
(
select id, max(time) as time
from yourtable
group by id) a inner join yourtable b on a.time = b.time and a.id = b.id
答案 1 :(得分:1)
您似乎希望该值具有最长时间。以下通常是一种有效的方法:
select t.*
from table t
where not exists (select 1 from table t2 where t2.id = t.id and t2.time > t.time);
这说的是:“从id
没有更大id
行的表格中获取所有行。使用table(id, time)
上的索引,这可能是表达此查询的最有效方式。
答案 2 :(得分:0)
select * from (
select id,[text],
ROW_NUMBER() OVER (PARTITION BY id ORDER BY [time] desc) as rn
from [table]) as cte
where rn = 1