## 4 replies

Last post Nov 06, 2015 09:26 AM by limno

Member

19 Points

164 Posts

### find maximum month of maximum year in sqlserver

dear all,

below is my data ,

MON           YEAR
01                2014
07                2014
12                2014
03                2015 and expected outputin '03' '2015' . but i get 12,2015 how do i do this?

year Month max

Star

9011 Points

2395 Posts

### Re: find maximum month of maximum year in sqlserver

Nov 06, 2015 03:55 AM|Lokesh B R|LINK

Hi,

` SELECT TOP 1  MAX([YEAR]), [MON] FROM [Table1] GROUP BY [YEAR], [MON] order by [YEAR] DESC`

year Month max

Mark the post as answer, if it helps you.

All-Star

50551 Points

14843 Posts

### Re: find maximum month of maximum year in sqlserver

`SELECT MAX([MON]), [YEAR] FROM [Table1] GROUP BY [YEAR] WHERE [YEAR]=MAX(YEAR)`

year Month max

Programming to simplify, don't look for hardway ...

Contributor

5774 Points

1173 Posts

### Re: find maximum month of maximum year in sqlserver

```declare @tab table (month tinyint, year smallint )
insert into @tab
values (1, 2014), (7, 2014), (12, 2014), (3, 2015)

select	top 1 *
from	@tab
order by year desc, month desc```

year Month max

Sandeep Mittal | Tech Blog : IT Developer Zone | Twitter : @itdeveloperzone | Facebook : @itdeveloperzone

• ### limno

All-Star

122063 Points

9650 Posts

Moderator

### Re: find maximum month of maximum year in sqlserver

```Create table test (Mon varchar(2),[Year] int)
Insert into test values('01',2014),('07',2014),('12',2014), ('03',2015)
Select  MOn,[Year]  from (
Select MOn,[Year]
, Row_NUmber() Over( Order BY Cast(Mon+'-01-'+Cast([Year] as Char(4)) as Datetime) DESC) rn from test) t
Where rn=1

drop table test```

year Month max

Jingyang Li
Format your SQL query with instant sql formatter:
http://www.dpriver.com/pp/sqlformat.htm