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