我的表是"统计"和列:
Id | user | date | product | number
示例数据:
1 | 1 | 29-11-2016 | 1 | 4
2 | 1 | 30-11-2016 | 1 | 3
3 | 2 | 30-11-2016 | 1 | 2
4 | 2 | 1-12-2016 | 1 | 2
现在我希望从任何用户的日期开始按照第一个数字(x)的顺序获得平均值,其中product = 1。
如果x = 1,则此数据查询应返回3(avg,其中id为1和3) 如果x = 2,则此数据查询应返回2,75(avg,其中id = 1,2,3和)
是否可以在一个查询中执行此操作?
答案 0 :(得分:0)
您的原始查询应为:
select avg(number) as average, user, date from statistic
where product = 1
order by date asc
group by user
在querybuilder中:
$query = $this->createQueryBuilder('s');
$query->select('AVG(s.number) as average, s.user, s.date')
->where('s.product = :product')
->setParameter('product', $product)
->addGroupBy('s.user')
->addOrderBy('s.date', 'asc');
//result will be an array
$result = $query->getQuery()->getResult();