Chinaunix

标题: 请教一个查询的写法,谢谢! [打印本页]

作者: coocool    时间: 2005-12-02 10:36
标题: 请教一个查询的写法,谢谢!
请教大家一个简单的问题,谢谢!
a               b                 c
1        2004.2.8  0:00           29.0                     
1        2004.2.13  0:00          28.0                     
1        2004.1.17  0:00          26.0                     
2        2004.8.21  0:00          25.0                     
2        2004.7.25  0:00          24.0                     
2        2004.1.24  0:00          40.0                     
2        2004.8.8  0:00           39.0                     
3        2004.5.10  0:00          38.0                     
3        2004.4.14  0:00          37.0   
这样的一个表怎么只取出1 2 3最后一个日期的数据?如下
1        2004.2.13  0:00          28.0                     
2        2004.8.21  0:00          25.0                     
3        2004.5.10  0:00          38.0

数据库是sybase 11.9.2

[ 本帖最后由 coocool 于 2005-12-2 11:05 编辑 ]
作者: 冷月无声    时间: 2005-12-02 10:59
有个笨方法,select distinct A 到一个游标里,然后定义变量移动游标查询select Max(B)
作者: zhhui2000    时间: 2005-12-02 11:11
也可以:

select a,max(b)"tb" into #tmp  from tab group by a

select ... from #tmp,tab where ....
作者: hannibal    时间: 2005-12-02 23:30
select  *  from  tbname  a
where  (select  count(*)
        from   tbname   b
        where  a.a =b.a
        and  a.b < b.b )<1
order by 1
作者: megaman    时间: 2005-12-03 10:14
select * from tbname group by a having b=max(b)
作者: coocool    时间: 2005-12-04 19:33
原帖由 冷月无声 于 2005-12-2 10:59 发表
有个笨方法,select distinct A 到一个游标里,然后定义变量移动游标查询select Max(B)


那意思就是还有个简单的办法喽?嘿嘿...

我也这么想的,利用游标,可是查询起来速度太慢了,不知道是不是我的写法有问题.
作者: coocool    时间: 2005-12-04 19:33
原帖由 megaman 于 2005-12-3 10:14 发表
select * from tbname group by a having b=max(b)



11.9.2不支持这个写法吧?
作者: chuxu    时间: 2005-12-04 21:35
应该支持吧




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2