1

I have two table Table A and Table B in mysql Database

Table A

id     date     advanced         payed_remaining        remaining_date
1    1/1/2018     400                 800                   4/1/2018
2    2/1/2018     600                 600                   3/1/2018
3    4/1/2018     800                 200                   6/1/2018
4    6/1/2018     400                 300                   8/1/2018
5    3/2/2018     600                 200                   6/2/2018
6    8/2/2018     800                 400                   10/2/2018

Table B

id     date      amount    
1    1/1/2018     900     
2    2/1/2018     600     
3    4/1/2018     300 
4    2/2/2018     400
5    5/2/2018     800 

Query for fetching monthly Data from both table,

$monthly_res = $con->prepare("SELECT t2.month, t2.total_advance, t2.total_pay_remaining, t1.total_income_amount FROM (SELECT  month(B.date) as month, SUM(B.income_amount) as total_income_amount FROM B group by month(B.date)) t1 INNER JOIN (SELECT month(A.date) as month, month(A.due_date) as month, t1.total_income_amount, SUM(A.advance) as total_advance, SUM(A.pay_remaining) as total_pay_remaining, t1.total_income_amount FROM A) t2 ON t2.month  = t1.month");
    $monthly_res->execute();
    while ($row = $monthly_res->fetch(PDO::FETCH_ASSOC)) {
        $month = $row['month'];
        $dt = DateTime::createFromFormat('!m', $month);
        $month_name = $dt->format('F');
        $total = $row['total_advance'] + $row['total_income_amount'] + $row['total_pay'];
        echo "<tbody>
            <tr>
                <td>".$month_name."</td>
                <td>".$total."/-</td>
            </tr>
        </tbody>";
    }

1054 - Unknown column 't1.total_income_amount' in 'field list'

when i remove t1.total_income_amount from query its showing December result only...

I want fetch monthly sum of advanced, sum of payed_remaining, sum of amount in one loop.

Result= sum(advanced) + sum(payed_remaining) + sum(amount) by month 

January : 5900

February : 3200

2 Answers 2

1

Can you try with below query

SELECT month(A.date) as month, SUM(A.advanced) as total_advance, SUM(A.payed_remaining) as total_pay_remaining, month(B.date) as month, SUM(B.income_amount) as total_income_amount 
From A
join B on month(A.date) = month(B.date)
group by month(A.date)
Sign up to request clarification or add additional context in comments.

Comments

1

You should join the aggregated result eg:

      SELECT t2.month
      , t2.total_advance
      , t2.total_pay_remaining
      , t1.total_income_amount FROM (
            SELECT  month(B.date) as month
              , SUM(B.income_amount) as total_income_amount 
            FROM B group by month(B.date)
      ) t1 
      INNER JOIN (  
            SELECT month(A.date) as month
              , SUM(A.advance) as total_advance
              , SUM(A.pay_remaining) as total_pay_remaining
            FROM A
      ) t2 ON t2.month  = t1.month

You should explict join clause and AND operator and join the sum of the b table

8 Comments

SELECT t2.month, t2.total_advance, t2.total_pay_remaining, t1.total_income_amount FROM ( SELECT month(B.date) as month SUM(B.income_amount) as total_income_amount FROM B group by month(B.date)) t1 INNER JOIN ( SELECT month(A.date) as month, month(A.remaining_date) as month, SUM(A.advanced) as total_advance, SUM(A.pay_remaining) as total_pay_remaining, t1.total_income_amount FROM A) t2 ON t2.month = t1.month
I try with above query but its showing syntax error
answer update .. eventually show me the exact error message
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'SUM(B.income_amount) as total_income_amount FROM income group by mon' at line 7
#1054 - Unknown column 't1.total_income_amount' in 'field list'
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.