YearMonth Value-------- ------201207 5000201208 4000
YearMonth Day Value--------- ------------ ------201207 01/07/2012 5000 divide by No Of Days in the month i.e 5000/31201207 02/07/2012 5000 divide by No Of Days in the month i.e 5000/31201207 03/07/2012 5000 divide by No Of Days in the month i.e 5000/31201207 04/07/2012 5000 divide by No Of Days in the month i.e 5000/31201207 05/07/2012 5000 divide by No Of Days in the month i.e 5000/31...201208 01/08/2012 4000 divide by No of Days in the month i.e. 4000/31
--===== Do this in a nice safe place that everyone has -- (You can build a permanent one in any database) USE TempDB; IF OBJECT_ID('TempDB..Tally','U') IS NOT NULL DROP TABLE Tally;GO--===================================================================-- Create a Tally table from 1 to 11000--===================================================================--===== Create and populate the Tally table on the fly. SELECT TOP 11000 IDENTITY(INT,1,1) AS N INTO dbo.Tally FROM Master.sys.ALL_Columns ac1 CROSS JOIN Master.sys.ALL_Columns ac2;--===== Add a CLUSTERED Primary Key to maximize performance ALTER TABLE dbo.Tally ADD CONSTRAINT PK_Tally_N PRIMARY KEY CLUSTERED (N) WITH FILLFACTOR = 100;--===== Allow the general public to use it GRANT SELECT ON dbo.Tally TO PUBLIC;GO
--===== Conditionally drop the test table to make reruns in SSMS easier. -- This is just a test table and is not a part of the solution. IF OBJECT_ID('tempdb..#YourTable','U') IS NOT NULL DROP TABLE #YourTable;--===== Create the test table. -- This is just a test table and is not a part of the solution. CREATE TABLE #YourTable ( YearMonth INT, [Value] INT );--===== Populate the table with test data. -- This is just test data and is not a part of the solution. INSERT INTO #YourTable (YearMonth,[Value]) SELECT 201207,5000 UNION ALL SELECT 201208,4000 UNION ALL SELECT 201201,3100 UNION ALL SELECT 200002,2900;--===== Solve the problem. SELECT YearMonth, [Day] = CONVERT(CHAR(10),DATEADD(mm,((YearMonth/100)-1900)*12 + YearMonth%100-1,0)+(t.N-1),103), [Value] = ([Value]+0.0)/DAY(DATEADD(mm,((YearMonth/100)-1900)*12 + YearMonth%100,0)-1) FROM #YourTable CROSS JOIN dbo.Tally t WHERE t.N <= DAY(DATEADD(mm,((YearMonth/100)-1900)*12 + YearMonth%100,0)-1) ORDER BY YearMonth, t.N;
--Create sample tablecreate table MonthlyData( YearMonth nchar(6), Value decimal(7))insert into MonthlyData Values ('201207',5000),('201208',4000)go--Query data;With DaysInMonth as( select YearMonth, Value, DATEFROMPARTS(cast(SUBSTRING(yearmonth,1,4) as int),cast(SUBSTRING(yearmonth,5,2) as int),1) as YearMonth_Date From MonthlyData),DailyData as( select YearMonth, Value, YearMonth_Date, 1 as DayCount, datepart(day,dateadd(month,datediff(month,'2012-01-31',YearMonth_Date),'2012-01-31')) as NumberOfDays, Value / datepart(day,dateadd(month,datediff(month,'2012-01-31',YearMonth_Date),'2012-01-31')) as DailyNumber from DaysInMonth union all Select YearMonth, Value, YearMonth_Date, d.DayCount + 1, NumberOfDays, d.DailyNumber From DailyData d where d.DayCount < NumberOfDays)select YearMonth,Convert(CHAR(10),dateadd(day,daycount-1,YearMonth_Date),103) as [Day], DailyNumber as [Value] from dailydataorder by YearMonth, DayCount
--Create sample tablecreate table MonthlyData( YearMonth nchar(6), Value decimal(7))insert into MonthlyData Values ('201207',5000),('201208',4000)go--Query data;With DaysInMonth as( select YearMonth, Value, DATEFROMPARTS(cast(SUBSTRING(yearmonth,1,4) as int),cast(SUBSTRING(yearmonth,5,2) as int),1) as YearMonth_Date From MonthlyData),DailyData as( select YearMonth, Value, YearMonth_Date, 1 as DayCount, datepart(day,dateadd(month,datediff(month,'2012-01-31',YearMonth_Date),'2012-01-31')) as NumberOfDays, Value / datepart(day,dateadd(month,datediff(month,'2012-01-31',YearMonth_Date),'2012-01-31')) as DailyNumber from DaysInMonth union all Select YearMonth, Value, YearMonth_Date, d.DayCount + 1, NumberOfDays, d.DailyNumber From DailyData d where d.DayCount < NumberOfDays)select YearMonth,Convert(VARCHAR(10),YearMonth_Date,103) as [Day], DailyNumber as [Value] from dailydataorder by YearMonth, DayCount