当前位置: 代码迷 >> Sql Server >> 这个结果用select如何查出来
  详细解决方案

这个结果用select如何查出来

热度:9   发布时间:2016-04-27 12:44:12.0
这个结果用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)
  相关解决方案