Note that there are some explanatory texts on larger screens.

plurals
  1. POA SELECT statement as subquery with two conditions
    primarykey
    data
    text
    <p>I'm working on a ECG data that basically has the following schema</p> <pre><code>Col 1 = TIMESTAMP Col 2 = PATIENTID Col 3 = ECGVALUE </code></pre> <p>Now, I'm trying to write a SQL statement that should be able to select all the rows that satisfy the following condition</p> <pre><code>Row index &gt;= n and TIMESTAMP of xth row &lt;= TIMESTAMP of nth row + offset </code></pre> <p>To explaing it further, let say I've following data in my database</p> <pre><code>1.1 ANON 1.1 1.3 ANON 2.3 3.5 ANON 4.3 5.0 ANON 6.5 6.3 ANON 7.5 7.9 ANON 8 8.6 ANON 9.4 </code></pre> <p>Now, I want to select the data from 3rd row till 3 seconds of data has been collected, which means that my resultset should have</p> <pre><code>3.5 ANON 2.3 *//3rd row till TIMESTAMP &lt;= 3.5 + 3 &lt;= 6.5* 1.3 ANON 2.3 3.5 ANON 4.3 5.0 ANON 6.5 6.3 ANON 7.5 </code></pre> <p>Last two rows are neglected as difference between TIMESTAMP of first and last cannont go beyond 3. So If I go back to my condition, which is</p> <pre><code>Row index &gt;= n and TIMESTAMP of xth row &lt;= TIMESTAMP of nth row + offset </code></pre> <p>Here, </p> <pre><code>n: nth row from where data must be selected x: Any arbitary row in result set offset: Maximum difference between first and last TIMESTAMP of result set. </code></pre> <p>I've written a working SQL statement for the above condition, but I think its not that much optimized as I'm new to SQL.</p> <pre><code>SELECT TIMESTAMP, ECGVALUE FROM ( SELECT TIMESTAMP, ECGVALUE, ROW_NUMBER() OVER() AS RN FROM EKLUND.DEV_RAWECG ) WHERE RN &gt;= n AND TIMESTAMP &lt;= ( SELECT TIMESTAMP FROM ( SELECT TIMESTAMP, ROW_NUMBER() OVER() AS TM ) WHERE TM = n ) + offset; </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