Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8843791
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T11:19:18+00:00 2026-06-14T11:19:18+00:00

In SQLite I have a table that is created like this (simplified): CREATE TABLE

  • 0

In SQLite I have a table that is created like this (simplified):

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);

Now I want to list combinations that match each other. Consider column local as local bank account and remote as remote bank account. Whenever a transaction is made from local to remote with value x and there exists a matching transaction which received the value x but as negative value (in relation to the first found), I want to have an output from SQLite.

My current approach is like this:

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)"

But this returns:

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

What I wanted as result would be this:

id|local|remote|value|id|local|remote|value
1|a|b|1|2|b|a|-1
4|a|d|2|6|d|a|-2

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 GROUP BY, but this didn’t work with q1.id and q2.id as parameter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T11:19:20+00:00Added an answer on June 14, 2026 at 11:19 am

    As there might be several rows with the same values for local/remote/value,
    the rank of the rows within each such set has to be calculated in order to pair the rows correctly.

    This is easier using other databases than sqlite (using row numbering primitives, CTEs etc), but in sqlite the correct result may be obtained like this:

    SELECT t1.id, t1.local, t1.remote, t1.value,
    t2.id, t2.local, t2.remote, t2.value FROM
    (SELECT q1.*, count(q1b.id) AS rank
    FROM entries q1 LEFT JOIN entries q1b
    ON q1.local = q1b.local AND q1.remote = q1b.remote
    AND q1.value = q1b.value AND q1.id >= q1b.id
    GROUP BY q1.id) AS t1,
    (SELECT q2.*, count(q2b.id) AS rank
    FROM entries q2 LEFT JOIN entries q2b
    ON q2.local = q2b.local AND q2.remote = q2b.remote
    AND q2.value = q2b.value AND q2.id >= q2b.id
    GROUP BY q2.id) AS t2
    WHERE t1.local = t2.remote AND t1.remote = t2.local
    AND t1.value = - t2.value
    AND t1.id < t2.id AND t1.rank = t2.rank
    

    See http://sqlfiddle.com/#!5/c684e/66/0

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my application I have a sqlite database that looks like this: CREATE TABLE
I have a table looking like this created with sqlite. CREATE TABLE Cars (
I have table like this: CREATE TABLE Date_Table (Year DATETIME , Month1 INTEGER, Month2
I have created an sqlite table using the following statement: CREATE TABLE IF NOT
Using sqlite3, I have a table that looks like this: +---------+-----------------+----------+-----------+--------+ | ArtId |
I have a table in SQLite: CREATE TABLE test_results(timestamp TEXT, npass INTEGER, nfails INTEGER)
I have a table that looks like this user_id | name | created_on |
I have a table for a Statistical project. Structure is like this: CREATE TABLE
i have this sqlite table: CREATE TABLE frames (videomd5 TEXT, framemd5 TEXT, type TEXT,
I'm using sqlite3 in python 2.5. I've created a table that looks like this:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.