Note that there are some explanatory texts on larger screens.

plurals
  1. POSQL query advice (replacing exists with joins)
    primarykey
    data
    text
    <p>I have table with following fields and indexes: <img src="https://i.stack.imgur.com/PBBZr.png" alt="enter image description here"><br> I need to make a query that will do:</p> <blockquote> <p>check table, if there are <strong>Dial</strong> events, after which (with same unique_id) no <strong>Bridge</strong> or <strong>Hangup</strong> events comes </p> </blockquote> <p><em>or</em> </p> <blockquote> <p>if there are <strong>NewCallerId</strong> events, with no <strong>Hangup</strong> events after it (with same unique_id) or no <strong>Dial</strong> events (with call_id = current unique_id)</p> </blockquote> <p>There was one query, but I didn't make it work, and users answers didn't help either. Here's the query that does the work in 395 seconds (too long):</p> <pre><code>SELECT t1.* FROM ASTERISK t1 WHERE EXISTS (SELECT t2.* FROM ASTERISK t2 WHERE t1.OPERATOR_DIAL = '$extension' AND t1.EVENT = 'Dial' AND NOT EXISTS (SELECT t3.* FROM ASTERISK t3 WHERE t3.UNIQUE_ID = t1.UNIQUE_ID AND ( t3.EVENT = 'Hangup' OR t3.EVENT = 'Bridge' ))) OR EXISTS (SELECT t4.* FROM ASTERISK t4 WHERE t1.EVENT = 'NewCallerid' AND t1.OPERATOR_DIAL = '$extension' AND NOT EXISTS (SELECT t5.* FROM ASTERISK t5 WHERE ( t5.UNIQUE_ID = t1.UNIQUE_ID AND t5.EVENT = 'Hangup' ) OR ( t5.CALL_ID = t1.UNIQUE_ID AND t5.EVENT = 'Dial' ))) ORDER BY DATE DESC LIMIT 1 </code></pre> <p>Tried to rewrite it with JOINS, but getting <em>#1248 - Every derived table must have its own alias</em></p> <p>This is my attempt with a JOIN query:</p> <pre><code>SELECT t1.* FROM asterisk t1 INNER JOIN (SELECT t2.* FROM asterisk t2 WHERE t1.operator_dial = '$extension' AND t1.event = 'Dial' LEFT OUTER JOIN (SELECT t3.* FROM asterisk t3 WHERE t3.unique_id = t1.unique_id AND (t3.event = 'Hangup' OR t3.event = 'Bridge'))) OUTER JOIN (SELECT t4.* FROM asterisk t4 WHERE t1.event = 'NewCallerid' AND t1.operator_dial = '$extension' LEFT OUTER JOIN (SELECT t5.* FROM asterisk t5 WHERE (t5.unique_id = t1.unique_id AND t5.event = 'Hangup') OR (t5.call_id = t1.unique_id AND t5.event = 'Dial'))) ORDER BY DATE DESC LIMIT 1 </code></pre> <p>To show, how the query should work I made 2 screenshots (top part has some calls that will not be in query result and bottom part is one call that must be in query result: <img src="https://i.imgur.com/8P9LLmF.png" alt="screen"></p>
    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