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.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Injecting fake historical data

Author  Topic 

flamz
Starting Member

21 Posts

Posted - 2008-02-27 : 12:26:29
Hello,
I have a table that holds historical data. I am currently debugging an application that reads this data and would like to work with some *fake*, generated historical data.

Here's my problem:
Each row in the historical table has a date, an ID and a list of values, like this:

DATE ID, val1, val2, ..., val3
02/27/2008 1, 15, 18, ..., 4
02/27/2008 2, 25, 16, ..., 2

I would like to generate new rows for each ID. Each new insert should DECREMENT all values in the record by 1 day for the date and by a random number for the others (except ID, of course). This way, I would end up with something like:

DATE ID, val1, val2, ..., val3
02/27/2008 1, 15, 18, ..., 4
02/26/2008 1, 14, 17, ..., 2
02/25/2008 1, 12, 15, ..., 0
02/24/2008 1, 7, 14, ..., 0
...

I wrote a program in C that does this, but it's very slow... (ie. I need to generate hundreds of thousands of rows). It uses the brute force approach: get the last historical data, read each field, decrement each one then insert a new record.

I think it may be possible to increase the speed by using some clever SQL, but I would need to some with this.

Any help is appreciated,
Cheers.



visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2008-02-27 : 12:44:29
INSERT INTO HistoryTable

SELECT DATEADD(d,-1 * b.number,a.DATE),
a.ID,
a.Val1-RandNo,
a.Val2-RandNo,
....

FROM HistoryTable a
CROSS JOIN master..spt_values b
WHERE b.type='p'
AND b.number BETWEEN 1 and 20

RandNo is random number generated using one of methods from here

http://weblogs.sqlteam.com/jeffs/archive/2004/11/22/2927.aspx
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2008-02-28 : 02:25:22
http://sqlblogcasts.com/blogs/madhivanan/archive/2007/10/10/generating-random-numbers-part-ii.aspx

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -