表a有2个字段
bh varchar(20)
date datetime记录中bh可以重复,但日期不同,用怎样的SQL语句提取出每一个编号最后一次登记的日期的数据呢?
例原数据:
11 2005-01-01
11 2006-04-01
22 2001-01-01
22 2003-09-02
需要得到的数据
11 2006-04-01
22 2003-09-02
SQL怎么写呀
bh varchar(20)
date datetime记录中bh可以重复,但日期不同,用怎样的SQL语句提取出每一个编号最后一次登记的日期的数据呢?
例原数据:
11 2005-01-01
11 2006-04-01
22 2001-01-01
22 2003-09-02
需要得到的数据
11 2006-04-01
22 2003-09-02
SQL怎么写呀
where date in
(
select max(date)
from a
group by bh
) as x
where not exists (select 1 from a where bh = t1.bh and rq > t1.rq)
select * from abc
where c2 in
(
select max(c2)
from abc
group by c1
)
union all select '22',cast('2001-01-01' as datetime) union all select '22',cast('2003-09-02' as datetime)select * from @taselect bh,max(date) from @ta group by bh
(select distant bh from 表) as b on a.bh=b.bh
group by bh
这样就可以了!