Victor Faria
Victor Faria

Reputation: 5

Using Mysql to select values from different tables with different names as credit and debit and calculate balance

I need to get values from different tables with different column names and select it as a common name that I could make a balance calculation. Something like that:

Table 1 (credit)

+-----------+--------------+---------------+
| ID_company|  date_table1 |  credit_table1|
+-----------+--------------+---------------+
|         1 |   2017-08-19 |            50 | 
|         1 |   2017-08-19 |           250 |
|         1 |   2017-08-20 |             0 |
+-----------+--------------+---------------+

Table 2 (credit)

+-----------+--------------+---------------+
| ID_company|  date_table2 |  credit_table2|
+-----------+--------------+---------------+
|         1 |   2017-08-19 |           150 | 
|         1 |   2017-08-19 |            50 |
|         1 |   2017-08-19 |             0 |
+-----------+--------------+---------------+

Table 3 (debit)

+-----------+--------------+---------------+
| ID_company|  date_table3 |   debit_table3|
+-----------+--------------+---------------+
|         1 |   2017-08-19 |            10 | 
|         1 |   2017-08-19 |            10 |
|         1 |   2017-08-20 |             0 |
+-----------+--------------+---------------+

Table 4 (debit)

+-----------+--------------+---------------+
| ID_company|  date_table4 |   debit_table4|
+-----------+--------------+---------------+
|         1 |   2017-08-19 |            10 | 
|         1 |   2017-08-19 |            10 |
|         1 |   2017-08-20 |             0 |
+-----------+--------------+---------------+

Result

+-----------+--------------+---------------+--------------+---------+
| ID_company|  date_grouped|   total_debit | total_credit | balance |
+-----------+--------------+---------------+--------------+---------+
|         1 |   2017-08-19 |            20 |          200 |     180 |
|         1 |   2017-08-19 |            20 |          300 |     280 | 
|         1 |   2017-08-20 |             0 |            0 |       0 |
+-----------+--------------+---------------+--------------+---------+

The relation between the tables is the ID_company, but the column names are different and I need to calculate the credit, debit and balance from all tables grouped by date.

Upvotes: 0

Views: 357

Answers (1)

Imanuel
Imanuel

Reputation: 3667

I think this should work, even if there is no entry for a given date in up to three tables.
Balance is per day, not accumulative.

It will not produce your example result, but I think there's an error in your example. Do you really need two rows for ID 1 and the same date?

SELECT 
    ID_company, 
    date_grouped, 
    sum(credit_table1) + sum(credit_table2) total_credits, 
    sum(debit_tabel3) + sum(debit_table4) total_debit, 
    sum(debit_tabel3) + sum(debit_table4)
        - (sum(credit_table1) + sum(credit_table2)) balance
FROM (
    SELECT 
        ID_Company, 
        date_table1 date_grouped, 
        credit_table1,
        0 credit_table2, 
        0 debit_table3, 
        0 debit_table4 
    FROM table1
    UNION
    SELECT 
        ID_Company, 
        date_table2 date_grouped, 
        0 credit_table1, 
        credit_table2,
        0 debit_table3, 
        0 debit_table4 
    FROM table2
    UNION
    SELECT 
        ID_Company, 
        date_table3 date_grouped, 
        0 credit_table1, 
        0 credit_table2, 
        debit_table3,
        0 debit_table4 
    FROM table3
    UNION
    SELECT 
        ID_Company, 
        date_table4 date_grouped, 
        0 credit_table1, 
        0 credit_table2, 
        0 debit_table3,
        debit_table4
    FROM table4  
)
GROUP BY ID_company, date_grouped

Upvotes: 1

Related Questions