Note that there are some explanatory texts on larger screens.

plurals
  1. POSorting data from tables according to time with a level of accuracy in datetime
    primarykey
    data
    text
    <p>I have two tables containing datetime and double. </p> <p>Which looks like this:</p> <pre><code>mysql&gt; select * from ONE; | mysql&gt; select * from TWO; +---------------------+----------+ | +---------------------+----------+ | date | value | | | date | value | +---------------------+----------+ | +---------------------+----------+ | 2002-03-18 10:30:02 | -181.241 | | | 2002-03-18 10:30:00 | -188.192 | | 2002-03-18 10:30:06 | -180.673 | | | 2002-03-18 10:30:04 | -187.619 | | 2002-03-18 10:30:10 | -180.055 | | | 2002-03-18 10:30:08 | -187.032 | | 2002-03-18 10:30:14 | -179.459 | | | 2002-03-18 10:30:12 | -186.418 | | 2002-03-18 10:30:18 | -178.801 | | | 2002-03-18 10:30:16 | -185.807 | </code></pre> <p>I'm trying to perform a query on the values from each of the tables with a common date column (using ONE.date as reference). However, in some cases the measurements does not have symmetric date. Above is an example of such a period (this is worst case scenario), here the values from table TWO corresponds to the values in table ONE two seconds later. </p> <p>In order to sort the values of the two tables according to the time, I want to allow a "sorting accuracy", set to be a maximum time difference between the measurements of one "event". This accuracy is in this case 2 seconds. </p> <p>How I wan't it to look: If the time difference between ONE.data and TWO.date is less than +- 2 seconds; join the value from TWO in ONE: </p> <pre><code>mysql&gt; select * from ONE; +---------------------+----------+-----------+ | date | valueONE | valueTWO | +---------------------+----------+ ----------+ | 2002-03-18 10:30:02 | -181.241 | -188.192 | | 2002-03-18 10:30:06 | -180.673 | -187.619 | | 2002-03-18 10:30:10 | -180.055 | -187.032 | | 2002-03-18 10:30:14 | -179.459 | -186.418 | | 2002-03-18 10:30:18 | -178.801 | -185.807 | </code></pre> <p>If the difference is more than +- 2 seconds, discard the measurement. This is what I have tried, however the query below just times out.</p> <pre><code>SELECT ONE.date, ONE.value, TWO.date, TWO.value FROM ONE join TWO on ONE.date = TWO.date+INTERVAL 1 SECOND or ONE.date = TWO.date </code></pre> <p>Is it possible to have a IF sentence inside the join statement such that if </p> <pre><code> if SELECT TIMESTAMPDIFF(SECOND,'2002-03-18 10:30:02','2002-03-18 10:30:00') &lt;= 2, then join </code></pre>
    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.
 

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