declare
@ttable
(id1
int,id2
int,a
int,b
int,c
int)
insert
into
@tselect1,
1,5,
4,3union
allselect2,
1,8,
6,4union
allselect1,
2,6,
7,2union
allselect2,
2,10,
5,4union
allselect1,
3,10,
1,1union
allselect2,
3,12,
2,2/*
-->取最小
select * from 表 a where 某列=(select min(某列) from 表 where 分組列=a.分組列)
-->取最大
select * from 表 a where 某列=(select max(某列) from 表 where 分組列=a.分組列)
-->取第一條
select * from 表 a where 某列=(select top 1 某列 from 表 where 分組列=a.分組列)
-->隨機取一條
select * from 表 a where 某列=(select top 1 某列 from 表 where 分組列=a.分組列 order by newid())
*/select
*from
@ta
wherea=
(select
max(a)
from
@twhere
id2=
a.id2)
order
byid2
sql 分組後重複資料取時間最新的一條記錄
max id 注意id必須使用聚合函式max pid,max time as desc 降序是為了where keyid 1 1是固定值第一條 如果公升序由於不知道每組多少條where中keyid就無法過濾了 as keyid,drop table if exists tmptable 存在表則刪除...
mysql 分組取最新一條
mysql 分組取最新一條 select from select from usr warn info handle order by handle time desc limit 10 t group by warn info idusr warn info handle 表名 handle ti...
Mysql 分組取最新一條
我有如下這張表的資料,需要根據mobile 號碼分組,每條 取最新的資料 方案1 select from select from model online forecastscore phone0001 order by id desc as cn group by cn.mobile 方案2 最佳...