关于一个很难的查询问题
有这样一个表 aTable
主键 号码 时间 地点
ID C_NO C_DT C_ADDR
1 2 2005-01-01 12:00:00 A
2 3 2003-11-01 12:00:00 A
3 3 2004-01-01 11:23:00 B
4 2 2005-04-15 02:12:00 V
5 1 2003-02-05 03:24:00 C
..........................
想要得到. 所有同一号的最新时间的所有记录集, (并且号码不允许重复)
例如:
ID C_NO C_DT C_ADDR
1 2 2005-01-01 12:00:00 A
3 3 2004-01-01 11:23:00 B
5 1 2003-02-05 03:24:00 C
100求救, 急需,, 谢谢 高手
问题点数:100、回复次数:6Top
1 楼hdhai9451(☆新人类☆)回复于 2005-08-03 08:40:30 得分 0
select a.*
from aTable a
inner join (select id,c_no,c_dt=max(c_dt) from aTable group by id,c_no)b
on a.id=b.id and a.c_no=b.c_no and a.c_dt=b.c_dt
Top
2 楼coolingpipe(冷箫轻笛)回复于 2005-08-03 08:44:56 得分 50
select a.*
from aTable a
inner join (select c_no,c_dt=max(c_dt) from aTable group by c_no)b
on a.c_no=b.c_no and a.c_dt=b.c_dt
Top
3 楼hdhai9451(☆新人类☆)回复于 2005-08-03 08:55:52 得分 50
更改一下
select a.*
from aTable a
inner join (select c_no,c_dt=max(c_dt) from aTable group by c_no)b
on a.c_no=b.c_no and a.c_dt=b.c_dt
按題意應該得到這個結果
3 3 2004-01-01 11:23:00.000 B
4 2 2005-04-15 02:12:00.000 V
5 1 2003-02-05 03:24:00.000 C
Top
4 楼wangang2436()回复于 2005-08-03 09:27:21 得分 0
由于表太多数据 达40万, 所以,select max(C_DT),C_NO from aTable group by C_NO
和select a.* from aTable a,(select max(C_DT),C_NO from aTable group by C_NO)t
where a.c_no=t.c_no and a.c_dt=t.c_dt
两者查询的数据结果,行数不一致, 哪一定有问题Top
5 楼coolingpipe(冷箫轻笛)回复于 2005-08-03 10:46:45 得分 0
行数不一致,肯定跟数据量的大小没有关系
你可以看一下aTable里有没有编码跟时间都相同的纪录,可以这样查找一下
select c_no,c_dt,count(1) from atable group by c_no,c_dt having count(1) >1
Top
6 楼libin_ftsafe(子陌红尘:TS for Banking Card)回复于 2005-08-03 10:56:59 得分 0
select
a.*
from
aTable a
where
not exists(select 1 from aTable where C_NO=a.C_NO and C_DT>a.C_DT)
Top




