有个问题请教,现在表里有三条记录,sfz(身份证),zp(照片信息),time
id sfz name zp time
1 123 a z1 18
2 123 a z2 23
3 123 a z3 12
4 456 b z4 5
5 456 b z5 10
现在我选取每个身份证对应的最新(即time最大)的那条记录所有字段的信息,也就是
2 123 a z2 23
5 456 b z5 10
用select * from t where romnum=1 group by sfz order by time desc
这样做不行,因为group by 有一个原则,就是 select 后面的所有列中,没有使用聚合函数的列,必须出现在 group by 后面,怎样我就取不到照片信息了,不知道哪个高手可以指点一下,别的方法也行,谢了!
PS:
select id,sfz,name,zp,time
from t
where (sfz,time) in
(select sfz,max(time) from t group by sfz having time = max(time)),这样可以
但是到数据库里查的时候太慢了,有没有优化查询的方法,或者其他呢?谢谢!
id sfz name zp time
1 123 a z1 18
2 123 a z2 23
3 123 a z3 12
4 456 b z4 5
5 456 b z5 10
现在我选取每个身份证对应的最新(即time最大)的那条记录所有字段的信息,也就是
2 123 a z2 23
5 456 b z5 10
用select * from t where romnum=1 group by sfz order by time desc
这样做不行,因为group by 有一个原则,就是 select 后面的所有列中,没有使用聚合函数的列,必须出现在 group by 后面,怎样我就取不到照片信息了,不知道哪个高手可以指点一下,别的方法也行,谢了!
PS:
select id,sfz,name,zp,time
from t
where (sfz,time) in
(select sfz,max(time) from t group by sfz having time = max(time)),这样可以
但是到数据库里查的时候太慢了,有没有优化查询的方法,或者其他呢?谢谢!
from
(select id, sfz, name ,zp ,time,
row_number() over(partition by sfz order by time desc) rn
from tb
)t
where rn=1;
--用分析函数,分组排序之后再取
select * from (
select id, sfz, name ,zp ,time,
row_number() over(partition by sfz order by time desc) rn
from t
)
where rn=1;
unable to extend temp segment by 128 in tablespace CZRK_GYZ_TEMP的问题
select id,sfz,name,zp,time
from t a
where exists(select sfz from t b where a.sfz=b.sfz group by sfz having a.time = max(b.time))
今天我也遇到你这个表空间不足问题,原因是你的操作造成大量的排序等,造成表空间CZRK_GYZ_TEM不足了,适当扩充下九OK了今天我遇到的是在对一个300多万的表上加一个index发生的临时表空间temp不足
select a.id,a.sfz,a.name,a.zp,a.time from t a
where exists(select sfz,max(time) from t group by sfz having a.sfz=sfz and a.time = max(time));
where not exists(
select 1 from t b where a.sfz=b.sfz and a.time<b.time
);