日期:2014-05-18  浏览次数:20546 次

这样的查询语句怎么写?
是这样的,有一个表a,里面的数据如下:
其中id   int,tatid   varchar(18)   workDate   varchar(16)

  id                   tatid                                       workDate
1090     200403050000000066                   2002-4--2003-11
1006     200403050000000066                   2003-11--2004-11
1007     200403050000000066                   2000-11--2002-4
1015     200403050000000075                   2002-6--2003-10
1016     200403050000000075                   NULL
1019     200403050000000075                   2004-11--2006-10
......

我想查出结果,如:

  id                   tatid                                       workDate
1006     200403050000000066                   2003-11--2004-11
1019     200403050000000075                   2004-11--2006-10
......


就是workDate最靠后的,请问怎么写?

------解决方案--------------------
使用时间倒序,top n不就行了?
------解决方案--------------------
select * from a order by workDate desc
------解决方案--------------------
说实话lz说的更让人迷糊了
------解决方案--------------------
select id,tatid,max(workDate) from a where tatid in (select distinct tatid from a)
------解决方案--------------------
select * from a as table1,
(select max(id) as maxid from a group by id) as table2
where table1.id = table1.id order by table1.id
------解决方案--------------------
忘了加上group by
------解决方案--------------------
select top 2 * from a order by workDate desc
------解决方案--------------------
select * from a where 不存在workDate比当前行大的tatid

大概就这个意思,用EXISTS多试一下。
------解决方案--------------------
select distinct tatid from a order by workDate desc
------解决方案--------------------
不好意思,刚才写错了,修正如下:

select * from a as table1,
(select id from a where worddate in (select max(workDate) from a group by tatid)) as table2
where table1.id = table1.id order by table1.id