1

这是我的学说 (1.2) 查询的第一种形式,页面底部是返回的内容。

$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);

我正在尝试这样做:

$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');

我预计两个用户 (assigned_to) 的回报是花费在与 assignment_to id 相关的所有问题上的所有时间的总和,与 time_estimate 相同。

但这就是返回的;

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

我是否在尝试正确的事情,我想要的输出是否可以仅通过 groupby 和聚合值来实现?还是我需要做一些特别的事情?

问候,

注意 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)
4

1 回答 1

0

好的,我相信我已经找到了解决方案。是不是最好的我不知道..

$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');

为所花费的时间添加子查询,而不是加入它会停止所有奇怪的数字。似乎直接加入它会导致我设置的组中出现重复的行..

于 2013-03-13T13:21:45.287 回答