SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Pivot Table Month Name Order
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

gecew
Starting Member

10 Posts

Posted - 12/02/2013 :  09:43:27  Show Profile  Reply with Quote
how to order by month name query returns Dec13,Mar14,Jan14,Nov13 .. etc
but i want to Nov13,Dec13,Jan14,Mar14 ... etc

declare @cols as varchar(max),
 @query as varchar(max)
set  @cols =STUFF((select ','+QUOTENAME(tb3.month) FROM 
( select distinct (DATENAME(MONTH,dtDate)+''+CONVERT(varchar(5),YEAR(dtDate)) ) as month
 from tableA) tb3 

FOR XML PATH(''), TYPE ).value('.','NVARCHAR(MAX)'),1,1,'')
set @query ='SELECT '+@cols +' from (SELECT DATENAME(MONTH,dtDate) + CONVERT(varchar(5),YEAR(dtDate)) as month,Price FROM tableA ) tb 
pivot ( sum(Price) for monthin('+@Cols+')) p 
'
exec(@query)

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/02/2013 :  10:05:05  Show Profile  Reply with Quote

declare @cols as varchar(max),
 @query as varchar(max)
set  @cols =STUFF((select ','+QUOTENAME(tb3.month) FROM 
( select distinct (DATENAME(MONTH,dtDate)+''+CONVERT(varchar(5),YEAR(dtDate)) ) as month,YEAR(dtDate) * 100 + MONTH(dtDate)AS MonthNo
 from tableA) tb3 
ORDER BY MonthNo
FOR XML PATH(''), TYPE ).value('.','NVARCHAR(MAX)'),1,1,'')
set @query ='SELECT '+@cols +' from (SELECT DATENAME(MONTH,dtDate) + CONVERT(varchar(5),YEAR(dtDate)) as month,Price FROM tableA ) tb 
pivot ( sum(Price) for monthin('+@Cols+')) p 
'
exec(@query)


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

gecew
Starting Member

10 Posts

Posted - 12/02/2013 :  10:35:09  Show Profile  Reply with Quote
@Visakh16 thx
Go to Top of Page

gecew
Starting Member

10 Posts

Posted - 12/03/2013 :  03:56:13  Show Profile  Reply with Quote
i can't grand total row how do it
Go to Top of Page

RickD
Slow But Sure Yak Herding Master

United Kingdom
3608 Posts

Posted - 12/03/2013 :  04:20:17  Show Profile  Reply with Quote
I would do grand totals in the reporting layer rather than here.
Go to Top of Page

gecew
Starting Member

10 Posts

Posted - 12/03/2013 :  04:33:46  Show Profile  Reply with Quote
i use datatable compute and works perfect thx
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 12/03/2013 :  06:22:22  Show Profile  Reply with Quote
quote:
Originally posted by gecew

i can't grand total row how do it


this is the way to do it within PIVOT just if you're interested


http://visakhm.blogspot.in/2012/04/display-total-rows-with-pivotting-in-t.html


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000