日期:2014-05-19  浏览次数:20511 次

******查询的小小问题*****

id               日期                     公司          
1         2006-12-25                   A        
2         2006-2-5                       A
3         2007-2-9                       A
4         2004-9-6                       B
5         2006-12-8                     B
6         2007-9-8                       C
7         2007-2-1                       A
每个公司选择出日期最大的一条记录,其余的舍弃
结果是
id               日期                     公司          
3         2007-2-9                       A
5         2006-12-8                     B
6         2007-9-8                       C
谢谢拉!!!!!
马上揭贴


------解决方案--------------------
select * from t a where not exists
(
select 1 from t where 公司=a.公司 and 日期> a.日期
)
------解决方案--------------------
select * from t a where not exists
(
select 1 from t where 公司=a.公司 and 日期> a.日期
)

------解决方案--------------------
--上面的语句不能防止当两个日期相同且都最大,只显示最后一条记录,所以稍改如下:
select * from t a where not exists
(
select 1 from t where 公司=a.公司 and 日期> =a.日期 and id> a.id
)