Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This results in an O(n) database query, and an O(n) greatest time search, for a total complexity of O(2n) (which, of course, is still O(n)):</p> <p>Use a count distinct command in SQL which will 'bin' items for you in minute increments.</p> <p>So you'd run the count query on this table:</p> <pre><code>time 1 2 4 3 3 2 4 1 3 2 </code></pre> <p>And it would return:</p> <pre><code>0 1 1 1 2 3 3 3 4 2 </code></pre> <p>By counting each item.</p> <p>I suspect you can do the same thing with your table, and bin them by the minute, then run an algorithm on that.</p> <pre><code>SELECT customer_name, COUNT(DISTINCT city) as "Distinct Cities" FROM customers GROUP BY customer_name; </code></pre> <p>From this tutorial on count: <a href="http://www.techonthenet.com/sql/count.php" rel="nofollow noreferrer">http://www.techonthenet.com/sql/count.php</a> (near the end).</p> <p>Here is a similar page from MySQL's manual: <a href="http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html" rel="nofollow noreferrer">http://dev.mysql.com/doc/refman/5.1/en/counting-rows.html</a></p> <p>So if you have a table with a timedate in it (to the minute, allowing binning to happen by minutes):</p> <pre><code>datetime (yyyymmddhhmm) 200901121435 200901121538 200901121435 200901121538 200901121435 200901121538 200901121538 200901121435 200901121435 200901121538 200901121435 200901121435 </code></pre> <p>Then the SQL</p> <pre><code>SELECT datetime, COUNT(DISTINCT datetime) as "Date Time" FROM post GROUP BY datetime; </code></pre> <p>should return</p> <pre><code>200901121435 7 200901121538 5 </code></pre> <p>You will still need to post process this, but the hard work of grouping and counting is done, and will only result in just over 500k rows per year (60 minutes, 24 hours, 365 days)</p> <p>The post processing would be:</p> <pre><code>Start at time T = first post time. Set greatestTime = T Sum all counts between T and T+one hour --&gt; currentHourCount and greatestHourCount While records exist past T+one hour Increment T by one minute. While the first element is prior to time T, subtract it while the last element is before time T+ one hour, add it If currentHourCount &gt; greatestHourCount then greatestHourCount = currentHourCount greatestTime = T end while </code></pre> <p>-Adam</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      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