April 7, 2011 at 12:02 pm
Hi everyone,
I have the following issue:
[Customer Name][Financial Year][No of Years][Total Value]
Customer A2010/11 3 30,000
Customer B2011/12 2 50,000
I would like the following:
Create records and increase financial years based on [No of Years] plus divide (Total Value) by (No of Years) to produce in above example 5 records in the following format:
[Customer Name][Financial Year][Total Value]
Customer A2011/1210,000
Customer A2012/1310,000
Customer A2013/1410,000
Customer B2012/1325,0000
Customer B2013/1425,0000
Any help will be highly appreciated
Many thanks in advance
April 7, 2011 at 2:29 pm
Hi,
There might be other easy solutions also.
/*Your data in to this table*/
declare @Temp table ([Customer_Name] varchar(50),[Financial_Year] varchar(25), [No_of_Years] int,[Total_Value] int)
insert @Temp values ('Customer A', '2010/11' ,3, 30000)
insert @Temp values ('Customer B', '2011/12', 2, 50000)
/*Your data with identity to this new table*/
declare @NewTemp table (Rid int identity,[Customer_Name] varchar(50),[Financial_Year] varchar(25), [No_of_Years] int,[Total_Value] int)
insert into @NewTemp
Select * from @Temp
--Select * from @NewTemp
declare @I int
declare @j-2 int
set @j-2=1
while @j-2<=(Select COUNT(Rid) from @NewTemp)
Begin
set @I=1
while @I<=(Select No_of_Years from @NewTemp where Rid=@j)
begin
select [Customer_Name],Convert(Varchar,Left(Financial_Year,4)+@i)
+'/'+ Convert(Varchar,Right(Financial_Year,2)+@i)[Financial_Year],
Total_Value/(Select No_of_Years from @NewTemp where Rid=@j) [Total_Value]
from @NewTemp where Rid=@j
set @I=@I+1
end
set @j-2=@J+1
end
Thanks
Parthi
April 7, 2011 at 2:36 pm
i forced your data to be integers so i could perform math on them, like adding a year; then used a Tally table to generate the results:
With MyData ([Customer Name],[Financial Year],[No of Years],[Total Value])
As
(
SELECT 'Customer A',2010,3,30000.00 UNION ALL
SELECT 'Customer B',2011,2,50000.00
), miniTally As
(
SELECT top 50 row_number() over (ORDER BY name) As N from sys.columns
)
select
MyData.[Customer Name],
(miniTally.N + MyData.[Financial Year]),
[Total Value] / [No of Years]
FROM MyData
INNER JOIN miniTally on (MyData.[No of Years] ) > = miniTally.N
ORDER BY
MyData.[Customer Name]
Lowell
April 8, 2011 at 7:11 am
Many thanks for your swift responses guys
I will have a go at both solutions and will let you know progress ind due course.
Hopefully it will be fine.
A
April 16, 2011 at 9:12 pm
adel.mutlak1 (4/8/2011)
Many thanks for your swift responses guysI will have a go at both solutions and will let you know progress ind due course.
Hopefully it will be fine.
A
It's been more than a week... how'd it all turn out?
--Jeff Moden
Change is inevitable... Change for the better is not.
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply