Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2008-04-13 : 09:51:14
|
Hi guys,The result of my query I am not able to get it sorted by months.Here is my query & the output.declare @tbl as table(date datetime,amt int)insert into @tblselect '2-jan-2008',100 union allselect '15-jan -2008',200 union allselect '20-jan -2008',500 union allselect '12-jan-2008',300 union allselect '02-feb-2008',100 union allselect '09-feb-2008',250 union allselect '03-mar-2008',500 union allselect '05-mar-2008',800select Months,SumAmt,MaxAmt,MinAmt from( select datename(mm,date)as Months,sum(amt)as SumAmt,min(amt)as MinAmt,max(amt)as MaxAmt,row_number()over(partition by datename(mm,date) order by datename(mm,date))as rowid from @tbl group by datename(mm,date))t The result is Months SumAmt MaxAmt MinAmt------------------------------ ----------- ----------- -----------February 350 250 100January 1100 500 100March 1300 800 500I want to get it sorted month wise i.e Jan,Feb,March.But I cannot find a way |
|
|
elancaster
A very urgent SQL Yakette
1208 Posts |
Posted - 2008-04-13 : 10:07:10
|
| don't sort by datename because it will come out alphabetical. order by datepart insteadEm |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-04-13 : 11:35:57
|
just add a column to get monthnumber and sort on it:-select MonthNames,SumAmt,MaxAmt,MinAmt from(select datepart(mm,date)as Months,datename(mm,date)as MonthNames,sum(amt)as SumAmt,min(amt)as MinAmt,max(amt)as MaxAmt,row_number()over(partition by datename(mm,date) order by datename(mm,date))as rowid from @tbl group by datename(mm,date))torder by t.Months |
 |
|
|
|
|
|
|
|