select a,max(date) from table group by a
解决方案 »
- 这段代码,为什么不对?
- 动态生产的SQL语句,varchar(8000)长度不够
- 有关趋建的sql书籍的疑问
- 这是个什么东西?
- 根据这段话写出where条件,应该怎么处理呢
- 问个查询效率问题,我两张表数据都是60万,我sql语句是:select * from test08 a where a.nbr not in(select nbr from disct@towe where
- 大神,你说你会这个SQL语句的
- 有关触发器中判断记录是否更新的问题
- c#连接数据库的古怪问题,很急人 但愿知道问题所在的朋友能给个帮助,谢谢!
- SQL 2008 如何获取存储过程返回数据 的 列名
- 请用一个SQL语句找出男女生相等的班级
- 1*33与6*10那个查询更快
select a.* from tablename a ,(select type ,max(date) as date from table group by type ) b
where a.type=b.type and a.date=b.date
select a.*
from tablename a join
( select type,max([date]) as mdate
from tablename
group by [type]
) b on a.[type]=b.[type] and a.[date]=b.mdate
insert into @t
select 1,'XXX','A','2003-2-1'
union all select 2,'XXX','A','2003-1-1'
union all select 3,'XXX','A','2003-1-1'
union all select 4,'XXX','B','2003-2-1'
union all select 5,'XXX','B','2003-1-1'
union all select 6,'XXX','B','2003-1-1'
union all select 7,'XXX','C','2003-2-1'
union all select 8,'XXX','C','2003-1-1'
union all select 9,'XXX','C','2003-1-1'--查询
select * from @t a
where id=(select top 1 id from @t where type=a.type order by date desc)/*--测试结果
ID Name Type date
----------- ---------- ---- -------------------------
1 XXX A 2003-02-01 00:00:00.000
4 XXX B 2003-02-01 00:00:00.000
7 XXX C 2003-02-01 00:00:00.000(所影响的行数为 3 行)
--*/
select type ,max(date),name from table group by type 就不行,说name这个字段没有包含再聚合函数中
where id=(select top 1 id from 表 where type=a.type order by date desc)