Muezz
04/26/2022, 8:12 PMtourdownunder
04/26/2022, 8:20 PMtourdownunder
04/26/2022, 8:21 PMMuezz
04/26/2022, 8:21 PMsql
SELECT
    db_transactions.deb_acc,
    coalesce(sum(db_transactions.amount),0) as "debit_total"
FROM
    public.db_transactions
GROUP BY
    db_transactions.deb_acc,
FULL JOIN 
    (SELECT
        db_transactions.cred_acc,
        coalesce(sum(db_transactions.amount),0) as "credit_total"
    FROM
        public.db_transactions)
    GROUP BY
        db_transactions.cred_acc;)
ON 
    db_transactions.deb_acc=db_transactions.cred_acc;Muezz
04/26/2022, 8:22 PMdeb_acc and the cred_acc are in the same tabletourdownunder
04/26/2022, 8:23 PMMuezz
04/26/2022, 8:23 PMMuezz
04/26/2022, 8:24 PMtourdownunder
04/26/2022, 8:26 PMtourdownunder
04/26/2022, 8:26 PMtourdownunder
04/26/2022, 8:27 PMMuezz
04/26/2022, 8:27 PMMuezz
04/26/2022, 8:28 PMsyntax error at or near "JOIN"tourdownunder
04/26/2022, 8:28 PMMuezz
04/26/2022, 8:28 PMtourdownunder
04/26/2022, 8:30 PMMuezz
04/26/2022, 8:32 PMtourdownunder
04/26/2022, 8:38 PMMuezz
04/26/2022, 8:47 PMsql
-- Creates the table with the least number of columns required
create table test_table (
  id uuid DEFAULT uuid_generate_v4 ()  primary key,
  deb_acc text NOT NULL,
  cred_acc text NOT NULL,
  amount numeric NOT NULL
);
-- Inserts random data
INSERT INTO test_table(deb_acc, cred_acc,amount)
VALUES ('acc_1', 'acc_2',5),
    ('acc_3', 'acc_4',10),
    ('acc_5', 'acc_2',15),
    ('acc_1', 'acc_3',20),
    ('acc_4', 'acc_1',25);
-- Fetches the total debits for each account
SELECT
    test_table.deb_acc,
    coalesce(sum(test_table.amount),0) as "debit_total"
FROM
    public.test_table
GROUP BY
    test_table.deb_acc;
-- -- Fetches the total credits for each account
-- SELECT
--     test_table.cred_acc,
--     coalesce(sum(test_table.amount),0) as "credit_total"
-- FROM
--     public.test_table
-- GROUP BY
--     test_table.cred_acc;
-- -- Should fetch the current balance of each account
-- SELECT
--     test_table.deb_acc,
--     coalesce(sum(test_table.amount),0) as "debit_total"
-- FROM
--     public.test_table
-- GROUP BY
--     test_table.deb_acc,
-- FULL JOIN 
--     (SELECT
--         test_table.cred_acc,
--         coalesce(sum(test_table.amount),0) as "credit_total"
--     FROM
--         public.test_table)
--     GROUP BY
--         test_table.cred_acc)
-- ON 
--     test_table.deb_acc=test_table.cred_acc;
I have included both SELECT statements that need to be subtracted to get the current account balance. You can uncomment and go ahead accordingly.tourdownunder
04/26/2022, 9:12 PMsql
SELECT
     debits.acc,  --- its the same doesn't matter choose one
     SUM(credits.total - debits.total)
 FROM
     (SELECT
         test_table.deb_acc acc,
         coalesce(sum(test_table.amount),0) as "total"
     FROM
         public.test_table
     group by 1) as debits
 FULL JOIN 
     (SELECT
         test_table.cred_acc acc,
         coalesce(sum(test_table.amount),0) as "total"
     FROM
         public.test_table
     group by 1) as credits
 ON 
     debits.acc=credits.acc
group by 1tourdownunder
04/26/2022, 9:13 PMtourdownunder
04/26/2022, 9:13 PMMuezz
04/26/2022, 9:15 PMtourdownunder
04/26/2022, 9:17 PMtourdownunder
04/26/2022, 9:17 PMsql
 SELECT
     coalesce(debits.acc, credits.acc),
     SUM(coalesce(credits.total, 0) - coalesce(debits.total, 0))
 FROM
     (SELECT
         test_table.deb_acc acc,
         coalesce(sum(test_table.amount),0) as "total"
     FROM
         public.test_table
     group by 1) as debits
 FULL JOIN 
     (SELECT
         test_table.cred_acc acc,
         coalesce(sum(test_table.amount),0) as "total"
     FROM
         public.test_table
     group by 1) as credits
 ON 
     debits.acc=credits.acc
group by 1Muezz
04/26/2022, 9:22 PMtourdownunder
04/26/2022, 9:24 PMtourdownunder
04/26/2022, 9:25 PMMuezz
04/26/2022, 9:26 PM