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

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

2012-04-13 
根据时间查询,时间最大的所有数据。比如A表:namedateA2012.01A2012.02A2012.03B2012.02B2012.03B2012.04C20

根据时间查询,时间最大的所有数据。
比如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])

热点排行