Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

 SQL Server Forums Profile | Active Topics | Members | Search | Forum FAQ
 All Forums  General SQL Server Forums  New to SQL Server Programming  Fiscal Year Totals Reply to Topic  Printer Friendly
Author  Topic

Vack
Aged Yak Warrior

USA
530 Posts

 Posted - 09/18/2013 :  14:48:55 I have the following script that calculates Sales by month and current year. We run a Fiscal year from April 1st thru March 31st. So April 2012 sales are considered Fiscal Year 2013. Is there a way I can alter this script to get Fiscal Year Totals?``` select ClassificationId, YEAR(inv_dt) as Year, cus_no, isnull(sum(case when month(inv_dt) = 4 then salesamt end),0) as 'Apr', isnull(sum(case when month(inv_dt) = 5 then salesamt end),0) as 'May', isnull(sum(case when month(inv_dt) = 6 then salesamt end),0) as 'Jun', isnull(sum(case when month(inv_dt) = 7 then salesamt end),0) as 'Jul', isnull(sum(case when month(inv_dt) = 8 then salesamt end),0) as 'Aug', isnull(sum(case when month(inv_dt) = 9 then salesamt end),0) as 'Sept', isnull(sum(case when month(inv_dt) = 10 then salesamt end),0) as 'Oct', isnull(sum(case when month(inv_dt) = 11 then salesamt end),0) as 'Nov', isnull(sum(case when month(inv_dt) = 12 then salesamt end),0) as 'Dec', isnull(sum(case when month(inv_dt) = 1 then salesamt end),0) as 'Jan', isnull(sum(case when month(inv_dt) = 2 then salesamt end),0) as 'Feb', isnull(sum(case when month(inv_dt) = 3 then salesamt end),0) as 'Mar' from QIVOEHDRHSTcR group by ClassificationId, YEAR(inv_dt), cus_no ```Data returned looks like the following. ``` ClassificationID Year Cus_no Apr May June .... 100 2012 100 \$23 \$30 \$400 100 2013 100 \$40 \$45 \$600 ```What I would need is anything greater than or equal to April to show in the next years row.

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

 Posted - 09/18/2013 :  14:57:32 Do you want a fiscal year total as another column in your existing result set? Perhaps instead of showing us what it does, you should show us what you want. Here are some links that can help you post your sample data and expected output so we can help you better:http://www.sqlservercentral.com/articles/Best+Practices/61537/http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx

SwePeso
Patron Saint of Lost Yaks

Sweden
30421 Posts

 Posted - 09/18/2013 :  15:06:19 ```-- Prepare sample data DECLARE @Sample TABLE ( theDate DATETIME NOT NULL, theAmount INT NOT NULL ); INSERT @Sample ( theDate, theAmount ) VALUES ('20120311', 1203), ('20120411', 1204), ('20120511', 1205), ('20120611', 1206), ('20120711', 1207), ('20120811', 1208), ('20120911', 1209), ('20121011', 1210), ('20121111', 1211), ('20121211', 1212), ('20130111', 1301), ('20130211', 1302), ('20130311', 1303), ('20130411', 1304); -- SwePeso WITH cteSource(theFiscalMonth, theMonth, theAmount) AS ( SELECT DATEADD(MONTH, DATEDIFF(MONTH, '18990401', theDate), '19000101') AS theFiscalMonth, DATEPART(MONTH, theDate) AS theMonth, theAmount FROM @Sample ) SELECT DATEPART(YEAR, theFiscalMonth) AS FiscalYear, SUM(theAmount) AS Total, SUM(CASE WHEN theMonth = 4 THEN theAmount ELSE 0 END) AS April, SUM(CASE WHEN theMonth = 5 THEN theAmount ELSE 0 END) AS May, SUM(CASE WHEN theMonth = 6 THEN theAmount ELSE 0 END) AS June, SUM(CASE WHEN theMonth = 7 THEN theAmount ELSE 0 END) AS July, SUM(CASE WHEN theMonth = 8 THEN theAmount ELSE 0 END) AS August, SUM(CASE WHEN theMonth = 9 THEN theAmount ELSE 0 END) AS September, SUM(CASE WHEN theMonth = 10 THEN theAmount ELSE 0 END) AS October, SUM(CASE WHEN theMonth = 11 THEN theAmount ELSE 0 END) AS November, SUM(CASE WHEN theMonth = 12 THEN theAmount ELSE 0 END) AS December, SUM(CASE WHEN theMonth = 1 THEN theAmount ELSE 0 END) AS January, SUM(CASE WHEN theMonth = 2 THEN theAmount ELSE 0 END) AS February, SUM(CASE WHEN theMonth = 3 THEN theAmount ELSE 0 END) AS March FROM cteSource GROUP BY DATEPART(YEAR, theFiscalMonth);```Microsoft SQL Server MVP, MCT, MCSE, MCSA, MCP, MCITP, MCTS, MCDBA
Topic
 Reply to Topic  Printer Friendly Jump To: Select Forum General SQL Server Forums       New to SQL Server Programming       New to SQL Server Administration       Script Library       Data Corruption Issues       Database Design and Application Architecture SQL Server 2012 Forums       Transact-SQL (2012)       SQL Server Administration (2012)       SSIS and Import/Export (2012)       Analysis Server and Reporting Services (2012)       Replication (2012)       Availability Groups and DR (2012)       Other SQL Server 2012 Topics SQL Server 2008 Forums       Transact-SQL (2008)       SQL Server Administration (2008)       SSIS and Import/Export (2008)       High Availability (2008)       Replication (2008)       Analysis Server and Reporting Services (2008)       Other SQL Server 2008 Topics SQL Server 2005 Forums       Transact-SQL (2005)       SQL Server Administration (2005)       .NET Inside SQL Server (2005)       SSIS and Import/Export (2005)       Service Broker (2005)       Replication (2005)       High Availability (2005)       Analysis Server and Reporting Services (2005)       Express Edition and Compact Edition (2005)       Other SQL Server Topics (2005) SQL Server 2000 Forums       SQL Server Development (2000)       SQL Server Administration (2000)       Import/Export (DTS) and Replication (2000)       Transact-SQL (2000)       Analysis Services (2000)       MSDE (2000) Development Tools       ASP.NET       Reporting Services Development       Other Development Tools Site Related Forums       Site Related Discussions       Article Discussion       Poll Discussion       The Yak Corral Other Forums       SQL Server 6.5 \ SQL Server 7.0       Other Topics       MS Access       ClearTrace Support Forum Old Forums       CLOSED - General SQL Server       CLOSED - SQL Server 2005/Yukon  -------------------- Home Active Topics Frequently Asked Questions Member Information Search Page
 SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC