E
E
Evgeny-app2014-08-28 07:15:45
MySQL
Evgeny-app, 2014-08-28 07:15:45

Is the sum of values ​​in the table incorrectly calculated (yii php)?

I created a table and displayed the values ​​there, but when summing these values, the result is somehow strange. Does anyone know what could be the issue?
d09ad8f5838b4104988c5d4f6206901d.png

foreach ($months as $key => $m)
            {
                if ($key > $max_month) break;
                
                $k = sprintf('%02.0d', ($key + 1));
                $fields .= ', '."\n SUM(".$m.'.active_count) as '.$m.'_active_count, SUM('.$m.'.active_sum) as '.$m.'_active_sum,
                              '."\n SUM(".$m.'.realized_count) as '.$m.'_realized_count, SUM('.$m.'.realized_sum) as '.$m.'_realized_sum';
                $joins .=  "\n"."LEFT JOIN tbl_report ".$m." ON ".$m.".date > '".$year."-".$k."-01 00:00:00' AND 
                    ".$m.".date < '".$year."-".$k."-31 24:59:59' AND 
                    ".$m.".fio = all_months.fio ";
            }

            $query = "SELECT DISTINCT ".$fields."
                      FROM tbl_report
                      ".$joins."
                      WHERE all_months.date > '".$year."-01-01 00:00:00' AND all_months.date < '".$year."-12-31 24:59:59'";
            //echo $query;
            $data['summary'] = Yii::app()->db->createCommand($query)->queryRow();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander N++, 2014-08-28
@sanchezzzhak

It's about the request, not the framework.
With such a query, you need to additionally use GROUP BY.
As a general rule, when using SUM and LEFT JOIN, you always need to group ... or use INNER JOIN

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question