首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

这个结果用select如何查出来

2012-05-23 
这个结果用select怎么查出来表 table字段a 字段b字段c1112012-1-1101112012-1-251112012-1-332222012-2-11

这个结果用select怎么查出来
表 table
字段a 字段b 字段c
111 2012-1-1 10
111 2012-1-2 5
111 2012-1-3 3
222 2012-2-1 10
222 2012-2-2 5
222 2012-2-3 8

想得到的结果
111 日期是最大的 2012-1-3 字段c 是 3
222 日期是最大的 2012-2-3 字段c 是 8

-a--------b--------c----
111 2012-1-3 3 -
------------------------
222 2012-2-3 8 -
------------------------

[解决办法]
select * from test a where a.字段b=(select max(字段b) from test b where a.字段a=b.字段a)
[解决办法]

SQL code
select t1.a,t1.b,t1.c from table t1 join (select a,max(b) as maxDate from table group by a) t2on t1.a=t2.a and t1.b=t2.maxDate
[解决办法]
SQL code
select  * from tb t where 字段b=(select max(字段b) from tb where 字段a=t.字段a)
[解决办法]
SQL code
select t.字段a,t.字段b,t.字段cfrom(select row_number() over(partition by 字段a order by 字段b desc) rn,字段a,字段b,字段c from tab) twhere t.rn=1
[解决办法]
SQL code
select * from table awhere not exists(select 1 from table b where a.字段a=b.字段b and a.字段b<b.字段b )
[解决办法]
select * from table where b in(select max(b) from table group by a)

热点排行