Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get balance according date and ID for every customer_id and currency_id using INNER JOIN

I have a table named as transaction_table:

CREATE TABLE transaction_table
  (
    _id INTEGER PRIMARY KEY AUTOINCREMENT,
    date TEXT,
    debit REAL,
    credit REAL,
    curr_id INTEGER,
    cus_id INTEGER,
    FOREIGN KEY (curr_id) REFERENCES currencies(_id) ON DELETE CASCADE,
    FOREIGN KEY (cus_id) REFERENCES customers(_id) ON DELETE CASCADE
  )

And assume this data in it:

_id  date                       debit    credit   curr_id    cus_id
-------------------------------------------------------------------
1   2022-12-08T00:00:00.000     10.0       0.0         1         1
2   2022-12-07T00:00:00.000      0.0      20.0         1         1
3   2022-12-06T00:00:00.000      0.0      30.0         1         1
4   2022-12-07T00:00:00.000     40.0       0.0         1         1
5   2022-12-08T00:00:00.000    100.0       0.0         1         1

So I just make SELECT statement that will get balance according date then _id but the balance is wrong:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

SELECT  t1._id,
    t1.date ,
    t1.description ,
    t1.debit ,
    t1.credit,
    SUM(t2.debit - t2.credit) as blnc,
    t1.curr_id,
    t1.cus_id
FROM transaction_table t1 INNER JOIN transaction_table t2
ON t2.curr_id = t1.curr_id AND t2.cus_id = t1.cus_id AND t2._id <= t1._id AND t2.date <= t1.date
GROUP BY t1._id
ORDER BY t1.date DESC, t1._id DESC;

the result:

 _id    date                        debit   credit   balance   curr_id   cus_id
  -----------------------------------------------------------------------------
   5    2022-12-08T00:00:00.000     100.0      0.0     100.0         1        1
   1    2022-12-08T00:00:00.000      10.0      0.0      10.0         1        1
   4    2022-12-07T00:00:00.000      40.0      0.0     -10.0         1        1
   2    2022-12-07T00:00:00.000       0.0     20.0     -20.0         1        1
   3    2022-12-06T00:00:00.000       0.0     30.0     -30.0         1        1

the result is wrong and it should be like this:

 _id    date                        debit   credit   balance   curr_id   cus_id
  -----------------------------------------------------------------------------
   5    2022-12-08T00:00:00.000     100.0      0.0     100.0         1        1
   1    2022-12-08T00:00:00.000      10.0      0.0       0.0         1        1
   4    2022-12-07T00:00:00.000      40.0      0.0     -10.0         1        1
   2    2022-12-07T00:00:00.000       0.0     20.0     -50.0         1        1
   3    2022-12-06T00:00:00.000       0.0     30.0     -30.0         1        1

which I can achieve it by this code:

SELECT _id,date, description, debit, credit,
   SUM(debit - credit) OVER (PARTITION BY curr_id, cus_id ORDER BY date, _id) blnc,
   curr_id, cus_id 
FROM transaction_table
order by date desc, _id desc;

but I don’t need it cause its not working in old android.
I just need to use INNER JOIN.

>Solution :

The conditions about the dates and the ids in the ON clause need correction:

SELECT t1._id,
       t1.date,
       t1.description,
       t1.debit,
       t1.credit,
       SUM(t2.debit - t2.credit) AS blnc,
       t1.curr_id,
       t1.cus_id
FROM transaction_table t1 INNER JOIN transaction_table t2
ON t2.curr_id = t1.curr_id AND t2.cus_id = t1.cus_id 
AND (t2.date < t1.date OR (t2.date = t1.date AND t2._id <= t1._id))
GROUP BY t1._id
ORDER BY t1.date DESC, t1._id DESC;

See the demo.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading