E
E
Evgeny-app2014-09-01 08:14:27
MySQL
Evgeny-app, 2014-09-01 08:14:27

How to write nested sub request (php yii)?

It is not possible to display the sums of already summed values ​​from the table, I think that the matter is in the query. It is necessary to write such a query with a nested subquery so that it would output summarized and display non-repeating values

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

2 answer(s)
M
Max, 2014-09-01
@AloneCoder

myeyes.jpg.to
Poor poor yii

L
LAV45, 2014-09-11
@LAV45

When designing a database, you need to take into account all the strengths and weaknesses of ActivRecord in Yii if you refuse it and plan to independently form queries using then the cards in your hands. No one else can help you except yourself.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question