|
I would like to thank you in advance for the opportunity to help solve my problem. I am an sql neewbie, with the following problem: The table zpData contains the daily high price for a specific number of days (eg. 100) for a specific number of instruments (stocks, commodities, etc) - about 15000 instruments. It has been modified (zpData_mod) so that each instrument is ordered by date, using the variable n. I am trying to achieve the following: After a defined interval (eg. 14 days), I want to calculate the maximum high price during the preceding 14 days. Following this, for the rest of the dates in the table relating to the instrument I want to calculate the maximum high price on the specified interval - a type of "moving" calculation. For example, if the interval is 5 days this would be the result I am aiming to obtain (for each instrument)
I have tried to achieve the above result using a nested while loop which utilizes a select and update query. The inner loop obtains the first maximum high for each instrument and the update query updates the newMax column of the zpData_mod table with the result. This seems to works correctly. Although it finds the required value, when I redirect the data to a seperate table (using insert into...), the maximum for each period is duplicated by the maximum number assigned to the inner loop. The outer loop, aims at repeating this process for the rest of the dates for each instrument. The problem is that the calculation is performed correctly, but in the zpData_mod table, the newMax field displays the data of the last iteration. All the previous data is not visible. Again, when I redirect the data to another table, I see that the calculations are performed as I had hoped they would be. Here is the rest of the code: Note: I have set the counters to work on a small number of data in the table for testing purposes. What am I doing wrong?
(comments are locked)
|
Dear Scot Thank you very much for your quick response. Trying to understand your compact code made me realise how much I have still to learn, and you are only half my age! Well Done! Can you help me a little further? a) Assuming the interval I am interested in calculating is 5 days, the first Max value that will update newMax will be on the 5th day. I would then like to calculate the previous 5 period high on the 6th day, the previous 5 period high on the 7th day and so forth. b) Why when I change the @IntervalDays variable from 5 to any other number, eg. 10 does the code return NULL for all values in newMax? And finally c) wouldn't it be easier to use n instead of tDate, given that the dates used in this database are weekdays and holidays are ommitted? Thanks again Regards Stephen
Aug 21 '12 at 03:44 PM
stephen.poulitsis
You're welcome. I'm 42 and still have a lot to learn! Site defaulted me to 20 and increases each year.. A. I think I am doing that according to your desired results in the question. Here are the results from my answer: B. Yes, all NULLs with this sample data C. You could use n, I didn't because I didn't add that column to my zpData_Mod table. I don't think the fact it only contains weekdays would make it easier.
Aug 21 '12 at 11:59 PM
Scot Hauder
(comments are locked)
|
|
Scot, as was to be expected, you were right! If the data contains dates as I had shown in the sample data then the code works like a charm. What I failed to explain was that the "real" data is stock/commodity/forex end of day data, where the dates refer only to weekdays (with a sprinkle of holidays interspersed). That is the reason I used the 'n'value - (something I had seen in Gabriel Prester's article - "Calculating moving averages with T-SQL, www.sqlservercentral.com). Unfortunately my understanding of SQL is like a short-sighted person trying to read a book - I get the general concept, but have to try really hard to understand some of the details - I have a long way to go! Using your code as a base I have come up with the following, which seems to work (it doesn't determine the max value for the first interval however, which isn't so much of a problem for what I want to further achieve): What do you think? Always grateful for your input Be Well, Stephen
(comments are locked)
|
|
Scot, this is a sample of the data that I'm usinglink text Remember I'm a copy-paste guy! I use the row-number to overcome the gaps in the dates. OK, I understand what you mean regarding the HAVING statement! I see now, you don't want the last 5 dates, you want the last 5 days with activity. I've edited my answer above.
Aug 22 '12 at 10:45 PM
Scot Hauder
Dear Scot, sorry for the delayed response! Thanks so much for all your help!
Aug 27 '12 at 04:23 PM
stephen.poulitsis
(comments are locked)
|


