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 |
|
ATG
Starting Member
35 Posts |
Posted - 2011-10-24 : 02:43:20
|
| First off, thanks for reading this.Here is my code:select CONVERT(varchar,Year(H.order_date)) +'-'+ CONVERT(varchar,Left(datename(mm,H.order_date),3)) as [Year-month],COUNT(D.order_id) as [NumberOrders], SUM(D.quantity*D.order_price) as[TotalSales]from bkorders.order_headers HLEFT JOIN bkorders.order_details D on D.order_id=H.order_idGROUP BY CONVERT(varchar,Year(H.order_date)) +'-'+ CONVERT(varchar,Left(datename(mm,H.order_date),3))Here is my output.Year-month NumberOrders TotalSales2010-Dec 11 3157.362010-Nov 15 3176.272010-Oct 17 8008.662011-Apr 11 5006.882011-Aug 39 5138.842011-Feb 12 23254.202011-Jan 12 5164.252011-Jul 10 1173.352011-Jun 8 430.382011-Mar 25 3187.392011-May 12 3581.932011-Sep 16 4281.22Whats happening is it's displaying the results in order of year and date in alpha order. I need it to sort by calender date rather that alpha.For instance,2010-Oct 17 8008.662010-Nov 15 3176.272010-Dec 11 3157.36...Any ideas? Thanks. |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2011-10-24 : 02:55:01
|
| Add this at the endORDER BY H.order_dateMadhivananFailing to plan is Planning to fail |
 |
|
|
ATG
Starting Member
35 Posts |
Posted - 2011-10-24 : 02:59:40
|
| I should have clarified a bit more. The number after the month is not the day. It's the quantity of orders. Currently the date is in VARCHAR format. I need it to order by date but display as '2011-Sep' for instance. |
 |
|
|
lionofdezert
Aged Yak Warrior
885 Posts |
Posted - 2011-10-24 : 03:31:10
|
| ORDER BY [Year-month]as column alias is used for order by cluase but when you need to cast it for correct sorting you have to useORDER BY CAST(CONVERT(varchar,Year(H.order_date)) +'-'+ CONVERT(varchar,Left(datename(mm,H.order_date),3)) AS DATETIME)--------------------------http://connectsql.com/ |
 |
|
|
ATG
Starting Member
35 Posts |
Posted - 2011-10-24 : 03:39:50
|
| I figured it out. I used a Common Table Expression. Here is my code.WITH Sales (YrMth,NumOrd,TotSale)as ( select CAST(convert(varchar(4),H.order_date,100) + convert(varchar(4),year(H.order_date))as datetime) as [Year-month], COUNT(D.order_id) as [NumberOrders], SUM(D.quantity*D.order_price) as[TotalSales] from bkorders.order_headers H LEFT JOIN bkorders.order_details D on D.order_id=H.order_id GROUP BY convert(varchar(4),H.order_date,100) + convert(varchar(4),year(H.order_date)) )select CONVERT(varchar,Year(Sales.YrMth))+'-'+Left(DateName(Month,Sales.YrMth),3), Sales.NumOrd, Sales.TotSalefrom SalesORDER BY Sales.YrMthThanks for the assistance. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-10-24 : 03:41:59
|
[code]select CONVERT(varchar(4), Year(order_dte)) + '-' + Left(datename(mm, order_dte),3) as [Year-month], [NumberOrders], [TotalSales]FROM( SELECT dateadd(month, datediff(month, 0, H.order_date), 0) as [order_dte], COUNT(D.order_id) as [NumberOrders], SUM(D.quantity*D.order_price) as [TotalSales] from bkorders.order_headers H LEFT JOIN bkorders.order_details D on D.order_id=H.order_id GROUP BY dateadd(month, datediff(month, 0, H.order_date), 0)) dORDER BY [order_dte][/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
lionofdezert
Aged Yak Warrior
885 Posts |
Posted - 2011-10-24 : 04:06:31
|
| I ll go with khtan, as according to your requriements DRIVED TABLE is better then CTE--------------------------http://connectsql.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2011-10-24 : 04:09:33
|
quote: Originally posted by ATG I should have clarified a bit more. The number after the month is not the day. It's the quantity of orders. Currently the date is in VARCHAR format. I need it to order by date but display as '2011-Sep' for instance.
thats the issuewhy should you use varchar if you know that you're storing dates?By not using appropriate datatypes, you're making manipulations difficults and causing lots of unwanted casts to be added in your code------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2011-10-24 : 07:07:58
|
| Also try usingORDER BY dateadd(month,datediff(month,0,H.order_date),0)MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|