日期:2014-05-17 浏览次数:20484 次
if object_id('t') is not null drop table t
create table t
(
name varchar(10),
year int,
month int
)
insert into t
select 'A',2011,3 union
select 'B',2011,7 union
select 'B',2011,5 union
select 'C',2011,9 union
select 'C',2013,8 union
select 'C',2013,5 union
select 'C',2013,3
go
with cte as
(
select t.name,max(cast(str(t.year,4)+'/'+right('0'+str(t.month,2),2)+'/01' as datetime)) date
from
t
group by t.name
)
select cte.name,DATENAME(YEAR,cte.date) year,DATENAME(MONTH,cte.date) month
from cte