Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find out which rows match without having multiple results per row?
    primarykey
    data
    text
    <p>In SQLite I have a table that is created like this (simplified):</p> <pre><code>CREATE TABLE [entries] ( [id] INTEGER NOT NULL PRIMARY KEY, [local] VARCHAR, [remote] VARCHAR, [value] INTEGER ); INSERT INTO entries (local, remote, value) VALUES ("a", "b", 1); INSERT INTO entries (local, remote, value) VALUES ("b", "a", -1); INSERT INTO entries (local, remote, value) VALUES ("b", "a", -1); INSERT INTO entries (local, remote, value) VALUES ("a", "d", 2); INSERT INTO entries (local, remote, value) VALUES ("a", "d", 2); INSERT INTO entries (local, remote, value) VALUES ("d", "a", -2); </code></pre> <p>Now I want to list combinations that match each other. Consider column <strong>local</strong> as local bank account and <strong>remote</strong> as remote bank account. Whenever a transaction is made from <strong>local</strong> to <strong>remote</strong> with value <strong>x</strong> and there exists a matching transaction which received the value <strong>x</strong> but as negative value (in relation to the first found), I want to have an output from SQLite.</p> <p>My current approach is like this:</p> <pre><code>sqlite3 -header demo.db \ "SELECT * FROM (SELECT * FROM entries) AS q1, (SELECT * FROM entries) AS q2 WHERE q1.local = q2.remote AND q1.remote = q2.local AND q1.value = (q2.value * -1)" </code></pre> <p>But this returns:</p> <pre><code>id|local|remote|value|id|local|remote|value 1|a|b|1|2|b|a|-1 1|a|b|1|3|b|a|-1 2|b|a|-1|1|a|b|1 3|b|a|-1|1|a|b|1 4|a|d|2|6|d|a|-2 5|a|d|2|6|d|a|-2 6|d|a|-2|4|a|d|2 6|d|a|-2|5|a|d|2 </code></pre> <p>What I wanted as result would be this:</p> <pre><code>id|local|remote|value|id|local|remote|value 1|a|b|1|2|b|a|-1 4|a|d|2|6|d|a|-2 </code></pre> <p>Lines which don't have a matching partner should not be displayed - every line could only match to at most one other transaction. I tried with <strong>GROUP BY</strong>, but this didn't work with <strong>q1.id</strong> and <strong>q2.id</strong> as parameter.</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