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 |
marcusn25
Yak Posting Veteran
56 Posts |
Posted - 2013-09-17 : 08:29:58
|
Hello--I need help in creating summary of monthly records {Feb >> Jan} -- This should be able to display different years based on date parameter picked--Month Feb Mar >> Oct Nov >> Jan (different year)--AMount 512 100 200 245 >> 50 Selecto.Amount,O.OrdeDate,Case when month(O.OrderDate) = 0 then o.amountElse '0'END AS January,Case when month (O.OrderDate) =1 Then o.amountElse '0'END As February,Case When month (O.OrderDate) = 2 then o.amountElse '0'End as March,Case When month (O.OrderDate) = 3 THEN o.amountElse '0'End as April,E.T.C >>>>> TO Jan (Following Years)FROM Orders as O--Many ThanksMarcus Night |
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-09-17 : 09:06:53
|
quote: Originally posted by marcusn25 Hello--I need help in creating summary of monthly records {Feb >> Jan} -- This should be able to display different years based on date parameter picked--Month Feb Mar >> Oct Nov >> Jan (different year)--AMount 512 100 200 245 >> 50 Selecto.Amount,O.OrdeDate,Case when month(O.OrderDate) = 0 then o.amountElse '0'END AS January,Case when month (O.OrderDate) =1 Then o.amountElse '0'END As February,Case When month (O.OrderDate) = 2 then o.amountElse '0'End as March,Case When month (O.OrderDate) = 3 THEN o.amountElse '0'End as April,E.T.C >>>>> TO Jan (Following Years)FROM Orders as O--Many ThanksM. NcubeM. Ncube
If you want a monthly amount, I would think that you need an aggregation (SUM) somewhere in there. Perhaps like this? SELECT DATEADD(mm,DATEDIFF(mm,0,o.OrderDate),0) AS [OrderMonthYear], SUM(o.Amount) AS AmountFROM Orders oGROUP BY DATEADD(mm,DATEDIFF(mm,0,o.OrderDate),0) |
|
|
marcusn25
Yak Posting Veteran
56 Posts |
Posted - 2013-09-17 : 12:05:22
|
Thank you, the grouping or summing up of [AMOUNT] will be handled in the report.A DateAdd funtion is deffinetley needed. I want to be able to distinguish between month and yearwhen the orderdate is selected [(Jan) [Year] >> Feb Following [Years]Marcus. Night |
|
|
James K
Master Smack Fu Yak Hacker
3873 Posts |
Posted - 2013-09-17 : 12:35:09
|
quote: Originally posted by marcusn25 Thank you, the grouping or summing up of [AMOUNT] will be handled in the report.A DateAdd funtion is deffinetley needed. I want to be able to distinguish between month and yearwhen the orderdate is selected [(Jan) [Year] >> Feb Following [Years]M. Ncube
Can you explain what that means with an example? "Distinguish between month and year when order date is selected"?If you want to get the year of a given date, you can use the YEAR function, or DATEPART function. |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-10-16 : 08:07:05
|
quote: Originally posted by marcusn25 Thank you, the grouping or summing up of [AMOUNT] will be handled in the report.A DateAdd funtion is deffinetley needed. I want to be able to distinguish between month and yearwhen the orderdate is selected [(Jan) [Year] >> Feb Following [Years]Marcus. Night
do you mean why ordering you need months correctly in columnslikeJan2012 Feb2012 Mar2012...Dec2012 Jan2013 Feb2013...in that case you just need to order based on year folloed by month values and then pivot based on that (or in reports use matrix with column group as year and month value)------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|