Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL Selecting rows at varying intervals
    primarykey
    data
    text
    <p>I've got a situation where I have a huge table, containing a huge number of rows, which looks like (for example):</p> <pre><code>id Timestamp Value 14574499 2011-09-28 08:33:32.020 99713.3000 14574521 2011-09-28 08:33:42.203 99713.3000 14574540 2011-09-28 08:33:47.017 99713.3000 14574559 2011-09-28 08:38:53.177 99720.3100 14574578 2011-09-28 08:38:58.713 99720.3100 14574597 2011-09-28 08:39:03.590 99720.3100 14574616 2011-09-28 08:39:08.950 99720.3100 14574635 2011-09-28 08:39:13.793 99720.3100 14574654 2011-09-28 08:39:19.063 99720.3100 14574673 2011-09-28 08:39:23.780 99720.3100 14574692 2011-09-28 08:39:29.167 99758.6400 14574711 2011-09-28 08:39:33.967 99758.6400 14574730 2011-09-28 08:39:40.803 99758.6400 14574749 2011-09-28 08:39:49.297 99758.6400 </code></pre> <p>Ok, so the rules are: The timestamps can be any n number of seconds apart, 5s, 30s, 60s etc, it varies depending on how old the record is (archiving takes place).</p> <p>I want to be able to query this table to select each nth row based on the timestamp.</p> <p>So for example:</p> <p>Select * from mytable where intervalBetweenTheRows = 30s</p> <p>(for the purposes of this question, based on the presumption the interval requested is always to a higher precision than available in the database)</p> <p>So, every nth row based on the time between each row</p> <p>Any ideas?!</p> <p>Karl</p> <p>For those of you who are interested, recursive CTE was actually quite slow, I thought of a slightly different method:</p> <pre><code>SELECT TOP 500 MIN(pvh.[TimeStamp]) as [TimeStamp], AVG(pvh.[Value]) as [Value] FROM PortfolioValueHistory pvh WHERE pvh.PortfolioID = @PortfolioID AND pvh.[TimeStamp] &gt;= @StartDate AND pvh.[TimeStamp] &lt;= @EndDate GROUP BY FLOOR(DateDiff(Second, '01/01/2011 00:00:00', pvh.[TimeStamp]) / @ResolutionInSeconds) ORDER BY [TimeStamp] ASC </code></pre> <p>I take the timestamp minus an arbitrary date to give a base int to work with, then floor and divide this by my desired resolution, I then group by this, taking the min timestamp (the first of that 'region' of stamps) and the average value for that 'period'.</p> <p>This is used to plot a graph of historical data, so the average value does me fine. </p> <p>This was the fastest execution based on the table size that I could come up with</p> <p>Thanks for your help all.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload