当前位置: 代码迷 >> Sql Server >> 根据时间查询,时间最大的所有数据。该如何处理
  详细解决方案

根据时间查询,时间最大的所有数据。该如何处理

热度:24   发布时间:2016-04-27 13:29:56.0
根据时间查询,时间最大的所有数据。
比如A表:

name date
A 2012.01
A 2012.02
A 2012.03

B 2012.02
B 2012.03
B 2012.04

C 2012.03
C 2012.04
C 2012.05

最终得到数据为:
A 2012.03
B 2012.04
C 2012.05

得到每个人时间最大的数据。
Sql语句该如何写呢?谢谢。

------解决方案--------------------

SQL code
select a.name,date a.from(select row_number()over(partition by name order by cast(right(date,2) as int) desc) as num,*from tbl)awhere num=1
------解决方案--------------------
SQL code
CREATE TABLE #t(NAME CHAR(2),dates DATE)insert into #tselect 'A', '201201' union allselect 'A', '201202' union allselect 'A', '201203' union allselect 'B', '201202' union allselect 'B', '201203' union allselect 'B', '201204' union allselect 'C', '201203' union allselect 'C', '201204' union allselect 'C', '201205'SELECT NAME,MAX(dates) FROM #tGROUP BY NAME
------解决方案--------------------
SQL code
select * from A twhere not exists(select 1 from name=t.name and [date]>t.[date])
------解决方案--------------------
探讨

SQL code
select * from A t
where not exists(select 1 from name=t.name and [date]>t.[date])
  相关解决方案