Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
[font=Tahoma]I am creating a database for managing budgets. A Department gets a certain amount of budget each year and chooses how they want it to be split eg: IT get £8,000 and want to split it over 8 periods - £1000 per period. The problem i am having is that i need to create a new row in a new table for each split. So I need it to check the field "number of splits" and create that amount of periods eg: Row 1 - number of splits = 8, 8 new rows are created with the budget for each of the months (£1000) also stored on that row. And if row 2 = 2 splits £8,000 is divided by 2, two new rows are created and £4000 is stored in each. so i should get something that looks like thisID Period Number Of Splits Budget this period1 1 8 £10001 2 8 £10001 3 8 £10001 4 8 £10001 5 8 £10001 6 8 £10001 7 8 £10001 8 8 £10002 1 2 £10002 2 2 £1000I need this to be flexible in case someone spends over/under one month.. but getting the above sorted is my priority. Any help for a beginner?[/font=Tahoma]
khtan
In (Som, Ni, Yak)
17689 Posts
Posted - 2011-05-24 : 06:18:34
use a number table or tally table. If you don't have one, you can also use F_TABLE_NUMBER_RANGE
declare @splits int, @amt decimal(20,2)select @splits = 7, @amt = 8000select *, amt = case when number <> @splits then convert(decimal(10,2), @amt / @splits) else @amt - convert(decimal(10,2), @amt / @splits) * (@splits - 1) endfrom numberswhere number between 1 and @splits