简体   繁体   中英

MYSQL GroupBy with joins and aggregate values

This is the first form of my doctrine (1.2) query and at the bottom of the page is what is returned.

$q = Doctrine_Query::create()
            ->select('i.id, i.time_estimate, i.assigned_to, ts.time_spent')
            ->from('Model_Issue i')
            ->innerJoin('i.User_2 user')
            ->leftJoin('i.IssuesTimeSpent ts')
            ->where('i.project_id = ?',$project_id);

I'm trying to do this with it :

$q = Doctrine_Query::create()
            ->select('i.id, sum(i.time_estimate) as time_estimate, i.assigned_to, sum(ts.time_spent) as time_spent')
            ->from('Model_Issue i')
            ->innerJoin('i.User_2 user')
            ->leftJoin('i.IssuesTimeSpent ts')
            ->where('i.project_id = ?',$project_id)
            ->groupBy('assigned_to');

I expected a return of two users (assigned_to) with the sum of all time spent on all issues related to the assigned_to id, the same with time_estimate.

But this is what's returned;

array
  0 => 
    array
      'id' => string '1' (length=1)
      'assigned_to' => string '1' (length=1)
      'time_estimate' => string '238' (length=3) <-  this is wrong
      'time_spent' => string '72' (length=2) <-  this is correct i believe
  1 => 
    array
      'id' => string '7' (length=1)
      'assigned_to' => string '2' (length=1)
      'time_estimate' => string '3' (length=1) <-  this is wrong
      'time_spent' => string '30' (length=2) <-  this is correct i believe

Am I trying the right things, can the output i want be achieved with only groupby and aggregate values? Or do i need to do something special?

Regards,

NB Full array before groupby / sums

 array
      0 => 
        array
          'id' => string '1' (length=1)
          'time_estimate' => string '4' (length=1)
          'assigned_to' => string '1' (length=1)
          'IssuesTimeSpent' => 
            array
              0 => 
                array
                  'id' => string '1' (length=1)
                  'time_spent' => string '2' (length=1)
              1 => 
                array
                  'id' => string '40' (length=2)
                  'time_spent' => string '1' (length=1)
      1 => 
        array
          'id' => string '2' (length=1)
          'time_estimate' => string '6' (length=1)
          'assigned_to' => string '1' (length=1)
          'IssuesTimeSpent' => 
            array
              0 => 
                array
                  'id' => string '2' (length=1)
                  'time_spent' => string '4' (length=1)
              1 => 
                array
                  'id' => string '5' (length=1)
                  'time_spent' => string '3' (length=1)
              2 => 
                array
                  'id' => string '6' (length=1)
                  'time_spent' => string '3' (length=1)
              3 => 
                array
                  'id' => string '8' (length=1)
                  'time_spent' => string '1' (length=1)
              4 => 
                array
                  'id' => string '9' (length=1)
                  'time_spent' => string '1' (length=1)
              5 => 
                array
                  'id' => string '10' (length=2)
                  'time_spent' => string '1' (length=1)
              6 => 
                array
                  'id' => string '11' (length=2)
                  'time_spent' => string '1' (length=1)
              7 => 
                array
                  'id' => string '12' (length=2)
                  'time_spent' => string '2' (length=1)
              8 => 
                array
                  'id' => string '13' (length=2)
                  'time_spent' => string '1' (length=1)
              9 => 
                array
                  'id' => string '14' (length=2)
                  'time_spent' => string '1' (length=1)
              10 => 
                array
                  'id' => string '15' (length=2)
                  'time_spent' => string '1' (length=1)
              11 => 
                array
                  'id' => string '16' (length=2)
                  'time_spent' => string '1' (length=1)
              12 => 
                array
                  'id' => string '17' (length=2)
                  'time_spent' => string '1' (length=1)
              13 => 
                array
                  'id' => string '18' (length=2)
                  'time_spent' => string '1' (length=1)
              14 => 
                array
                  'id' => string '19' (length=2)
                  'time_spent' => string '1' (length=1)
              15 => 
                array
                  'id' => string '20' (length=2)
                  'time_spent' => string '1' (length=1)
              16 => 
                array
                  'id' => string '21' (length=2)
                  'time_spent' => string '1' (length=1)
              17 => 
                array
                  'id' => string '22' (length=2)
                  'time_spent' => string '1' (length=1)
              18 => 
                array
                  'id' => string '23' (length=2)
                  'time_spent' => string '1' (length=1)
              19 => 
                array
                  'id' => string '24' (length=2)
                  'time_spent' => string '1' (length=1)
              20 => 
                array
                  'id' => string '25' (length=2)
                  'time_spent' => string '1' (length=1)
              21 => 
                array
                  'id' => string '26' (length=2)
                  'time_spent' => string '1' (length=1)
              22 => 
                array
                  'id' => string '27' (length=2)
                  'time_spent' => string '1' (length=1)
              23 => 
                array
                  'id' => string '28' (length=2)
                  'time_spent' => string '1' (length=1)
              24 => 
                array
                  'id' => string '29' (length=2)
                  'time_spent' => string '1' (length=1)
              25 => 
                array
                  'id' => string '30' (length=2)
                  'time_spent' => string '1' (length=1)
              26 => 
                array
                  'id' => string '31' (length=2)
                  'time_spent' => string '1' (length=1)
              27 => 
                array
                  'id' => string '32' (length=2)
                  'time_spent' => string '1' (length=1)
              28 => 
                array
                  'id' => string '33' (length=2)
                  'time_spent' => string '1' (length=1)
              29 => 
                array
                  'id' => string '34' (length=2)
                  'time_spent' => string '1' (length=1)
              30 => 
                array
                  'id' => string '35' (length=2)
                  'time_spent' => string '1' (length=1)
              31 => 
                array
                  'id' => string '36' (length=2)
                  'time_spent' => string '1' (length=1)
              32 => 
                array
                  'id' => string '37' (length=2)
                  'time_spent' => string '1' (length=1)
              33 => 
                array
                  'id' => string '38' (length=2)
                  'time_spent' => string '1' (length=1)
              34 => 
                array
                  'id' => string '39' (length=2)
                  'time_spent' => string '3' (length=1)
              35 => 
                array
                  'id' => string '42' (length=2)
                  'time_spent' => string '1' (length=1)
              36 => 
                array
                  'id' => string '43' (length=2)
                  'time_spent' => string '10' (length=2)
      2 => 
        array
          'id' => string '3' (length=1)
          'time_estimate' => string '4' (length=1)
          'assigned_to' => string '1' (length=1)
          'IssuesTimeSpent' => 
            array
              0 => 
                array
                  'id' => string '3' (length=1)
                  'time_spent' => string '3' (length=1)
              1 => 
                array
                  'id' => string '41' (length=2)
                  'time_spent' => string '10' (length=2)
      3 => 
        array
          'id' => string '7' (length=1)
          'time_estimate' => string '3' (length=1)
          'assigned_to' => string '2' (length=1)
          'IssuesTimeSpent' => 
            array
              0 => 
                array
                  'id' => string '7' (length=1)
                  'time_spent' => string '30' (length=2)

Ok, I believe I have found a solution. Whether it's the best one I don't know..

$q = Doctrine_Query::create()
            ->select('i.id, assigned.full_name as full_name, count(i.id) as issue_count, sum(i.time_estimate) as time_estimate, 
                (SELECT sum(ts.time_spent) as t FROM Model_IssuesTimeSpent ts WHERE ts.user_id = i.assigned_to) as time_spent')
            ->from('Model_Issue i')
            ->leftJoin('i.User_2 assigned')
            ->where('i.project_id = ?',$project_id)
            ->groupBy('assigned_to');

Adding the subquery for the timespent, rather than joining it stops all the strange numbers. It seems joining it directly was causing duplicate rows within the groups i was setting..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM