数据库中有一字段存在重复,我想查出时间为最后的这个字段,并统计存在多少条这样的记录(题目说的可能不太清楚,帖子内容会补充)
数据库是这样的,存在一个字段a(此字段有重复),一个状态字段b,一个时间字段(插入时间)
我想查出比如,状态b为1的并且时间是最新数据(因为a有重复,所以根据时间要筛选出最新的,保证不同的a只是最新的一条),这样的数据在数据库中存在几条(存在多少条a,当然a是不重复的)
不知道我又没有说清楚
[解决办法]
意思大致听明白了,又是同组取一条的问题。
只是不知道你想要的数据结果是怎么样的,因为你说要得到什么统计多少条记录.这个不太明。
同组选一条参见: 例1
http://topic.csdn.net/u/20080123/18/9731d130-0d4b-4c11-8d89-f2c3ca331f0c.html
另外,请把你的示意数据,及希望得到的结果贴上来。
[解决办法]
没看懂,不是同组取一条的问题吧
[解决办法]
declare @a table(a int ,b int,c int)insert into @a values(101,1,2002)insert into @a values(101,1,2003)insert into @a values(101,2,2004)insert into @a values(102,1,2002)insert into @a values(102,2,2003)select a,b,max(c)as 插入时间from @awhere b = 1group by a,b
[解决办法]
create table tb(a int,b int,c int)insert into tb select 101,1,2002insert into tb select 101,1,2003insert into tb select 101,2,2004insert into tb select 102,1,2002insert into tb select 102,2,2003select * from (select * from tb where b=1) t where not exists(select 1 from tb where b=1 and a=t.a and c>t.c) select a,b,max(c)from tbwhere b=1group by a,bselect * from tb twhere c in(select max(c) from tb where a=t.a and b=1)
[解决办法]
select t.* from tb t where b = 1 and 时间字段 = (select max(时间字段) from tb where b = 1 and a = t.a)
[解决办法]
[Quote=引用:]
select t.* from tb t where b = 1 and 时间字段 = (select max(时间字段) from tb where b = 1 and a = t.a)