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
 SQL Server 2005 Forums
 Transact-SQL (2005)
 [Montly Summary Records]
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

marcusn25
Yak Posting Veteran

Germany
53 Posts

Posted - 09/17/2013 :  08:29:58  Show Profile  Reply with Quote
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

Edited by - marcusn25 on 10/16/2013 06:38:00

James K
Flowing Fount of Yak Knowledge

3744 Posts

Posted - 09/17/2013 :  09:06:53  Show Profile  Reply with Quote
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

Germany
53 Posts

Posted - 09/17/2013 :  12:05:22  Show Profile  Reply with Quote
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

Edited by - marcusn25 on 10/16/2013 06:39:46
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3744 Posts

Posted - 09/17/2013 :  12:35:09  Show Profile  Reply with Quote
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

India
52325 Posts

Posted - 10/16/2013 :  08:07:05  Show Profile  Reply with Quote
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
  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