Question : Running Total for fiscal year

I have a function that sets the fiscal year to start on 7/1 regardless of the year.  I have a stored procedure that takes the value from the row MCRRRMDCD and increments MCRYTDRRMCDC by whatever number is in its row.  An example of the data is below.

TDATE                       MCRRRMCDC      MCRYTDRRMCDC
2010-01-05 00:00:00.000      NULL      NULL
2010-01-04 00:00:00.000      NULL      NULL
2010-01-03 00:00:00.000      NULL      NULL
2010-01-02 00:00:00.000      NULL      NULL
2010-01-01 00:00:00.000      NULL      NULL
2009-12-31 00:00:00.000      1      95
2009-12-30 00:00:00.000      1      94
2009-12-29 00:00:00.000      NULL      93
2009-12-28 00:00:00.000      NULL      93
2009-12-27 00:00:00.000      NULL      93
2009-12-26 00:00:00.000      NULL      93
2009-12-25 00:00:00.000      NULL      93
2009-12-24 00:00:00.000      NULL      93
2009-12-23 00:00:00.000      2      93
2009-12-22 00:00:00.000      NULL      91

What my problem is - starting on 1/1/2010, it stops incrementing...  It worked for all previous years.

Here is the function FiscalYear:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER FUNCTION [dbo].[FiscalYear](@date DATETIME) RETURNS INT AS
BEGIN
  DECLARE @fiscalyear INT
  SET @fiscalyear = YEAR(@date) + (CASE WHEN MONTH(@date) >= 7 THEN 1 ELSE 0 END)
  RETURN @fiscalyear
END

Here is the stored procedure that is being used to populate a table that is being updated with new data daily:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spDlyHIHMasterMCRYTDRRMCDC]
AS
BEGIN
--IF EXISTS DROP TABLE TEST1

--IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'[dbo].[tablename]')
--AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
--DROP TABLE [dbo].[TEST1];
--CREATE TABLE [dbo].[tablename] ( columns specification );

DROP TABLE TEST1
--BEGIN
CREATE TABLE TEST1 ([TDATE] datetime, MCRRRMCDC smallint,MCRYTDRRMCDC smallint)
CREATE CLUSTERED INDEX ndxTDate on TEST1([TDATE])

;WITH data AS ( SELECT CONVERT(VARCHAR(4), [TDATE], 120) ym, [TDATE], MCRRRMCDC, ROW_NUMBER() OVER ( PARTITION BY dbo.fiscalyear([TDATE]) ORDER BY [TDATE]) r
FROM dbo.tblHIHMaster)
insert into TEST1
SELECT data.[TDATE], data.MCRRRMCDC
, (SELECT SUM(x.MCRRRMCDC) FROM data x WHERE x.ym = data.ym AND x.r <= data.r ) MCRYTDRRMCDC
 FROM data
 UPDATE tblHIHMaster SET MCRYTDRRMCDC = ISNULL(TEST1.MCRYTDRRMCDC,0) FROM TEST1 WHERE tblHIHMaster.TDATE = TEST1.TDATE
END

Need assistance in figuring out why MCRYTDRRMCDC in table TEST1 stops incrementing from 1/1/2010 going forward...

Answer : Running Total for fiscal year

The problem appears to be in your YM column in your CTE, change it to use the fiscalyear function on the date, it was spliting your fiscal years in half and not working with the x.ym = data.ym in your subselect:
CONVERT(VARCHAR(4), dbo.fiscalyear([TDATE]), 120) ym
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[spDlyHIHMasterMCRYTDRRMCDC] 
AS
BEGIN
--IF EXISTS DROP TABLE TEST1

--IF EXISTS (SELECT * FROM sysobjects WHERE id = object_id(N'[dbo].[tablename]')
--AND OBJECTPROPERTY(id, N'IsUserTable') = 1)
--DROP TABLE [dbo].[TEST1];
--CREATE TABLE [dbo].[tablename] ( columns specification );

DROP TABLE TEST1
--BEGIN
CREATE TABLE TEST1 ([TDATE] datetime, MCRRRMCDC smallint,MCRYTDRRMCDC smallint)
CREATE CLUSTERED INDEX ndxTDate on TEST1([TDATE])

;WITH data AS ( SELECT CONVERT(VARCHAR(4), dbo.fiscalyear([TDATE]), 120) ym, [TDATE], MCRRRMCDC, ROW_NUMBER() OVER ( PARTITION BY dbo.fiscalyear([TDATE]) ORDER BY [TDATE]) r 
FROM dbo.tblHIHMaster)
insert into TEST1
SELECT data.[TDATE], data.MCRRRMCDC
, (SELECT SUM(x.MCRRRMCDC) FROM data x WHERE x.ym = data.ym AND x.r <= data.r ) MCRYTDRRMCDC
 FROM data
 UPDATE tblHIHMaster SET MCRYTDRRMCDC = ISNULL(TEST1.MCRYTDRRMCDC,0) FROM TEST1 WHERE tblHIHMaster.TDATE = TEST1.TDATE
END
GO
Random Solutions  
 
programming4us programming4us