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 |
junkocplcomsg
Starting Member
6 Posts |
Posted - 2010-05-12 : 21:55:16
|
Appreciate your assistanceMy FX Data (similar to Stock data) comes in one minute intervals, and each item contains a date, open, high, low and close (OHLC), I need to be able to convert this data to intervals for 5, 15, 60 120 240 minutes intervalsMy data looks like thiscdCurPairID cdDbfxTime cdCurOpen cdCurHigh cdCurLow cdCurClose14 10.05.10 05:12:00 1.432 1.4322 1.4314 1.431814 10.05.10 05:11:00 1.4319 1.4322 1.4317 1.431814 10.05.10 05:10:00 1.4314 1.432 1.4314 1.431814 10.05.10 05:09:00 1.4318 1.432 1.4313 1.431414 10.05.10 05:08:00 1.4315 1.432 1.4315 1.431814 10.05.10 05:07:00 1.4321 1.4323 1.4316 1.431614 10.05.10 05:06:00 1.4325 1.4326 1.432 1.432214 10.05.10 05:05:00 1.4323 1.4327 1.4322 1.432514 10.05.10 05:04:00 1.4323 1.4323 1.432 1.432114 10.05.10 05:03:00 1.4326 1.4326 1.4323 1.432414 10.05.10 05:02:00 1.4323 1.4327 1.4322 1.432514 10.05.10 05:01:00 1.4321 1.433 1.4321 1.432214 10.05.10 05:00:00 1.4325 1.4326 1.4319 1.43214 10.05.10 04:59:00 1.4323 1.4326 1.4323 1.432614 10.05.10 04:58:00 1.4327 1.4327 1.4324 1.432414 10.05.10 04:57:00 1.4323 1.4326 1.4323 1.432614 10.05.10 04:56:00 1.4327 1.4328 1.4323 1.432314 10.05.10 04:55:00 1.4326 1.4334 1.4325 1.432914 10.05.10 04:54:00 1.4329 1.433 1.4327 1.432714 10.05.10 04:53:00 1.4329 1.4329 1.4328 1.4329 which I have implemented as follows://ALTER PROCEDURE dbo.backup_spIntervalMinMaxClose2@intCurID integer,@intIntervalInMinutes integer,@dtFromDate datetime,@intMaxRows integerAS set rowcount @intMaxRows SELECT b.IntervalTime, a.cdDbfxTime, a.cdCurPairID, a.cdCurClose, b.cdCurHigh, b.cdCurLowFROM dbo.tblCurrencyData a INNER JOIN (SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, cdDbfxTime) / @intIntervalInMinutes * @intIntervalInMinutes , 0) AS IntervalTime, MAX(cdDbfxTime) as MaxTime, Min(cdDbfxTime) as minTime MIN(cdCurLow) AS cdCurLow, MAX(cdCurHigh) AS cdCurHigh, cdCurPairID FROM dbo.tblCurrencyData WHERE (cdCurPairID = @intCurID) AND (cdDbfxTime > @dtFromDate) GROUP BY DATEADD(MINUTE, DATEDIFF(MINUTE, 0, cdDbfxTime) / @intIntervalInMinutes * @intIntervalInMinutes , 0), cdCurPairID) b ON a.cdCurPairID = b.cdCurPairID AND a.cdDbfxTime = b.MaxTimeorder by cdDbfxTime desc //This returned dataset is only correct for the High, Low, and Close, but need as well the "open" value to be correctif the last few lines in my SQL code is change from:"ON a.cdCurPairID = b.cdCurPairID AND a.cdDbfxTime = b.MaxTimechanged to "ON a.cdCurPairID = b.cdCurPairID AND a.cdDbfxTime = b.MinTime"then I am getting the correct "open" value (If I add as well "a.cdCurClose") - but then I am missing the "close" valueso basically I needThe Open value from b.minTimeandthe Close value from b.MaxTimeI should appreciate your assistance. Thanks in Advance/Lars
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
junkocplcomsg
Starting Member
6 Posts |
Posted - 2010-05-13 : 22:17:57
|
Dear Visakh16,Thank you for the link and your suggestion; I must admit that I did not fully understand your blog entry nor its logic. It appeared to be give a similar result as my own above attempt.If I wanted totals I would simply amend my above query to include as well as-> b.TotalTransactions (in outer query)-> SUM(cdTransactions) as TotalTransactions (the inner query) (my table includes as well "cdTransactions" field, but you could otherwise similarly count e.g. the cdDbfxTime fields)like thisSELECT b.IntervalTime, a.cdDbfxTime, a.cdCurPairID, a.cdCurClose, b.cdCurHigh, b.cdCurLow, b.TotalTransactionsFROM dbo.tblCurrencyData a INNER JOIN (SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, cdDbfxTime) / @intIntervalInMinutes * @intIntervalInMinutes , 0) AS IntervalTime, MAX(cdDbfxTime) AS MaxTime, MIN(cdCurLow) AS cdCurLow, MAX(cdCurHigh) AS cdCurHigh, cdCurPairID, SUM(cdTransactions) as TotalTransactions FROM dbo.tblCurrencyData WHERE (cdCurPairID = @intCurID) AND (cdDbfxTime > @dtFromDate) GROUP BY DATEADD(MINUTE, DATEDIFF(MINUTE, 0, cdDbfxTime) / @intIntervalInMinutes * @intIntervalInMinutes , 0), cdCurPairID) b ON a.cdCurPairID = b.cdCurPairID AND a.cdDbfxTime = b.MaxTimeorder by cdDbfxTime desc as this point in time I am inclined to pull all my data from my SQL server into dataset (vb.net) and the make a custom function there, as do not need to return anything back to the sql server. |
|
|
junkocplcomsg
Starting Member
6 Posts |
Posted - 2010-05-13 : 23:39:04
|
I have added a yet another query to my existings queries-> This below query does work, if anyone can optimize this, I am all ears.ALTER PROCEDURE dbo.spIntervalMinMaxCloseVolume4@intCurID integer,@intIntervalInMinutes integer,@dtFromDate datetime,@intMaxRows integer AS set rowcount @intMaxRows SELECT TOP 100 PERCENT b.IntervalTime, a.cdDbfxTime, a.cdCurPairID, tblCurrencyData_2.cdCurOpen AS cdCurOpen2, b.cdCurHigh, b.cdCurLow, a.cdCurClose, b.TotalTransactionsFROM dbo.tblCurrencyData a INNER JOIN (SELECT DATEADD(MINUTE, DATEDIFF(MINUTE, 0, cdDbfxTime) / @intIntervalInMinutes * @intIntervalInMinutes, 0) AS IntervalTime, MAX(cdDbfxTime) AS MaxTime, Min(cdDbfxTime) AS MinTime, MIN(cdCurLow) AS cdCurLow, MAX(cdCurHigh) AS cdCurHigh, cdCurPairID, SUM(cdTransactions) AS TotalTransactions FROM dbo.tblCurrencyData WHERE (cdCurPairID = @intCurID) AND (cdDbfxTime > @dtFromDate) GROUP BY DATEADD(MINUTE, DATEDIFF(MINUTE, 0, cdDbfxTime) / @intIntervalInMinutes * @intIntervalInMinutes, 0), cdCurPairID) b ON a.cdCurPairID = b.cdCurPairID AND a.cdDbfxTime = b.MaxTime INNER JOIN (SELECT cdCurOpen, cdCurPairID, cdDbfxTime FROM dbo.tblCurrencyData WHERE (cdCurPairID = @intCurID) AND (cdDbfxTime > @dtFromDate)) tblCurrencyData_2 ON b.MinTime = tblCurrencyData_2.cdDbfxTime AND a.cdCurPairID = tblCurrencyData_2.cdCurPairIDORDER BY a.cdDbfxTime DESC //Lars |
|
|
|
|
|
|
|