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.
| Author |
Topic |
|
cmrrive
Starting Member
6 Posts |
Posted - 2012-03-01 : 14:24:00
|
| I have a table which contains 3 fieldBus_structure,Date,Amount1,2012-01-01,7580141,2012-02-01,8502141,2012-03-01,9502471,2012-04-01,100325Basically I have one entry per month I need to create a loop that take bus_structure 1 and first month and replicate bus_structure,and amount*day for each day on the month.Also i have 4 differentes structures code(1,2,3,4), can someone help me pleasemiguel |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-03-01 : 16:07:38
|
| You probably don't want to use a loop.However I don't really understand what you *do* want to do.can you give some example? maybe show use what the final result should be?I think you want a running total of some kind but I'm not sure.Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2012-03-02 : 04:16:31
|
| ah I see. they want to expand the table to include a row for every day in the month and have a multiple of the first day * <n> where n is the day in the month.You don't need a loop for that.You need a calendar table. Then a simple insert and then an update.However, whether it's a good idea or not..............Charlie===============================================================Msg 3903, Level 16, State 1, Line 1736The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-03-02 : 04:57:30
|
[code]DECLARE @Sample TABLE ( Bus INT, oldDate DATE, Amount INT )INSERT @SampleVALUES (1, '20120101', 758014), (1, '20120201', 850214), (1, '20120301', 950247), (1, '20120401', 100325);WITH cteSource(Bus, oldDate, newDate, theDay, Amount)AS ( SELECT s.Bus, oldDate, DATEADD(DAY, v.Number, s.oldDate) AS newDate, v.Number + 1 AS theDay, s.Amount FROM @Sample AS s INNER JOIN master.dbo.spt_values AS v ON v.Type = 'P' AND v.Number BETWEEN 0 AND 30)SELECT Bus, newDate, theDay, Amount, theDay * Amount AS DayAmountFROM cteSourceWHERE DATEDIFF(MONTH, oldDate, newDate) = 0ORDER BY Bus, newDate[/code] N 56°04'39.26"E 12°55'05.63" |
 |
|
|
cmrrive
Starting Member
6 Posts |
Posted - 2012-03-02 : 08:07:59
|
| Thanks for all the replies. Here is the scenario for the final results I'm looking forBus_structure, Date, Amount1,2012-01-01,7580141,2012-02-01,758014*2 (2ND DAY)1,2012-02-01,758014*3 (3RD DAY)AND SO ON UNTIL 2012-31-01,758014*31NOW TAKE FEB DATA1,2012-01-02,8502141,2012-02-02,850214*21,2012-02-03,850214*3AND SO ON UNTIL 2012-02-29,850214*29SAME THING FOR THE REST OF THE MONTHmiguel |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2012-03-02 : 08:10:59
|
?Did you even try my suggestion? N 56°04'39.26"E 12°55'05.63" |
 |
|
|
cmrrive
Starting Member
6 Posts |
Posted - 2012-03-05 : 10:10:53
|
quote: Originally posted by SwePeso ?Did you even try my suggestion? N 56°04'39.26"E 12°55'05.63"
Thanks a lot for you help this works perfect!!!!miguel |
 |
|
|
|
|
|
|
|