How to solve this Query Issue. [Resolved]

Posted by Jayakumars under ASP.NET MVC on 2/13/2017 | Points: 10 | Views : 166 | Status : [Member] [MVP] | Replies : 1
Hi

How to show this records using Sql Query

--My Data This
Sno Name Monthname
1 aa SEP 2014
2 bb MAY 2016
3 cc MAY 2015
4 dd MAY 2014
5 ee DEC 2016
6 ff DEC 2015
7 gg DEC 2014
8 hh DEC 2013

--I need Output this
Sno Name Monthname
1 bb MAY 2016
2 ee DEC 2016
3 cc MAY 2015
4 ff DEC 2015
5 dd MAY 2014
6 aa SEP 2014
7 gg DEC 2014
8 hh DEC 2013


I tried Monthname Desc But My Output not correct
How to use Descending Data for this output
Based on Month and Year based
in sql Server Query only.

Mark as Answer if its helpful to you


Responses

Posted by: Rajnilari2015 on: 2/13/2017 [Member] [Microsoft_MVP] [MVP] Platinum | Points: 50

Up
0
Down

Resolved
@Jayakumars , Please try the below

DECLARE @T TABLE(SlNo INT IDENTITY,Name VARCHAR(10),MonthName VARCHAR(20))
INSERT INTO @T VALUES
('aa','SEP 2014'),('bb','MAY 2016'),('cc','MAY 2015'),('dd','MAY 2014')
,('ee','DEC 2016'),('ff','DEC 2015'),('gg','DEC 2014'),('hh','DEC 2013')


SELECT
SlNo = ROW_NUMBER() OVER(ORDER BY YEAR(MonthName) DESC, MONTH(MonthName))
,Name
,MonthName
FROM @T


Result
-----------
SlNo	Name	MonthName
1 bb MAY 2016
2 ee DEC 2016
3 cc MAY 2015
4 ff DEC 2015
5 dd MAY 2014
6 aa SEP 2014
7 gg DEC 2014
8 hh DEC 2013


Hope that helps.

--
Thanks & Regards,
RNA Team

Jayakumars, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response