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.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 [Montly Summary Records]

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

Select
o.Amount,
O.OrdeDate,
Case when month(O.OrderDate) = 0 then o.amount
Else '0'
END AS January,
Case when month (O.OrderDate) =1 Then o.amount
Else '0'
END As February,
Case When month (O.OrderDate) = 2 then o.amount
Else '0'
End as March,
Case When month (O.OrderDate) = 3 THEN o.amount
Else '0'
End as April,
E.T.C >>>>> TO Jan (Following Years)

FROM Orders as O

--Many Thanks




Marcus 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

Select
o.Amount,
O.OrdeDate,
Case when month(O.OrderDate) = 0 then o.amount
Else '0'
END AS January,
Case when month (O.OrderDate) =1 Then o.amount
Else '0'
END As February,
Case When month (O.OrderDate) = 2 then o.amount
Else '0'
End as March,
Case When month (O.OrderDate) = 3 THEN o.amount
Else '0'
End as April,
E.T.C >>>>> TO Jan (Following Years)

FROM Orders as O

--Many Thanks




M. Ncube

M. 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 Amount
FROM
Orders o
GROUP BY
DATEADD(mm,DATEDIFF(mm,0,o.OrderDate),0)
Go to Top of Page

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 year
when the orderdate is selected [(Jan) [Year] >> Feb Following [Years]

Marcus. Night
Go to Top of Page

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 year
when 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.
Go to Top of Page

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 year
when the orderdate is selected [(Jan) [Year] >> Feb Following [Years]

Marcus. Night


do you mean why ordering you need months correctly in columns
like

Jan2012 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 MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page
   

- Advertisement -