Note that there are some explanatory texts on larger screens.

plurals
  1. POJoin derived table with itself
    primarykey
    data
    text
    <p>In short words:</p> <p>I have events table: [tbl]</p> <pre><code>time | newState 1200 | 1 1300 | 2 1400 | 5 </code></pre> <p>I need to transform this table into intervals table [intervals]:</p> <pre><code>t0 | t1 | state 1200 | 1300 | 1 1300 | 1400 | 2 </code></pre> <p>RESTRICTION: SQL COMPACT EDITION</p> <p>the query:</p> <pre><code>SELECT leftPart.time AS t0, min(rightPart.time) AS t1, leftPart.newState FROM tbl AS leftPart LEFT OUTER JOIN tbl As rightPart ON leftPart.time&lt;rightPart.time GROUP BY leftPart.Time,leftPart.newState </code></pre> <p>It works perfect when [tbl] is permanent table in database, but in my case [tbl] is derived from another select subquery! like this:</p> <pre><code>(SELECT time,newState from ...) AS derivedTb </code></pre> <p>So, when i try something like this:</p> <pre><code>SELECT derivedTbl.time As t0,derivedTbl.state,min(rigthTblPart.time) FROM (SELECT time,newState from ...) AS derivedTbl LEFT OUTER JOIN with derivedTbl AS rigthTblPart ON derivedTbl.Time&lt;rightTblPart.Time ... </code></pre> <p>It throws error: "derivedTbl does not exist"...</p> <p>It seems that the derived table under it's alias is not visible to higher level query (thanks, google translator! ))...</p> <p>Is there any way to store derived tables in query and using them in different parts of query? SQL CE doesn't support temporary tables, views and common table expressions...</p> <p><strong>task details (if interesting):</strong> i have 2 tables of events:</p> <pre><code>[states1] time | state1 1200 | 1 1300 | 2 1400 | 3 [states2] time | state2 1200 | 0 1230 | 10 1330 | 20 1430 | 30 </code></pre> <p>I need convert them in intervals table:</p> <pre><code>[intervals] t0 t1 state1 state2 1200 1230 1 0 1230 1300 1 10 1300 1330 2 10 1330 1400 2 20 1400 1430 3 20 1430 NULL 3 30 </code></pre> <p>stages of convertion: 1. overall timeline</p> <pre><code>(SELECT Time FROM States1 UNION SELECT Time FROM States2) AS timetbl </code></pre> <ol> <li>join states1 table</li> <li>join states2 table</li> </ol> <p>for this moment all goes well:</p> <pre><code>SELECT states12tbl.time, states12tbl.state1, states2tbl.State AS state2 FROM (SELECT states12tbl_1.time, states12tbl_1.state1, MAX(states2tbl.Time) AS states2time FROM (SELECT timetbl.time, states1tbl.State AS state1 FROM (SELECT timetbl_1.Time AS time, MAX(States1tbl.Time) AS state1time FROM (SELECT Time FROM States1 UNION SELECT Time FROM States2) AS timetbl_1 LEFT OUTER JOIN States1 AS States1tbl ON States1tbl.Time &lt;= timetbl_1.Time GROUP BY timetbl_1.Time) AS timetbl INNER JOIN States1 AS states1tbl ON timetbl.state1time = states1tbl.Time </code></pre> <ol> <li>join table with itself... and here is problem, i need to join code(3) with itself, because sql ce can't remember temp tables... If you have some better idea, please, share :)</li> </ol>
    singulars
    1. This table or related slice is empty.
    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