DQL/SQL 按月分组()
DQL/SQL GROUP BY MONTH()
是否可以按月 GROUP BY 只有日期作为一列?这可以通过 PHP 循环来实现,但我更愿意使用 SQL。实时代码如下:
$query = $this->em
->createQuery("SELECT Month(p.planDate), SUM(ROUND(p.ndata5/1000)) AS volume "
. "FROM NTPBundle:ParagonData p WHERE p.planDate >= :startDate AND p.ndata5<>0 GROUP BY Month(p.planDate) ORDER BY p.planDate")
->setParameter('startDate', $this->startDate->format('Y-m-d'));
正确排序,您必须命名 select 然后按此名称排序,例如:
$query = $this->em
->createQuery("SELECT Month(p.planDate) as monthPlan, SUM(ROUND(p.ndata5/1000)) AS volume "
. "FROM NTPBundle:ParagonData p WHERE p.planDate >= :startDate AND p.ndata5<>0 "
. "GROUP BY monthPlan "
. "ORDER BY p.planDate")
->setParameter('startDate', $this->startDate->format('Y-m-d'));
感谢提供线索
是否可以按月 GROUP BY 只有日期作为一列?这可以通过 PHP 循环来实现,但我更愿意使用 SQL。实时代码如下:
$query = $this->em
->createQuery("SELECT Month(p.planDate), SUM(ROUND(p.ndata5/1000)) AS volume "
. "FROM NTPBundle:ParagonData p WHERE p.planDate >= :startDate AND p.ndata5<>0 GROUP BY Month(p.planDate) ORDER BY p.planDate")
->setParameter('startDate', $this->startDate->format('Y-m-d'));
正确排序,您必须命名 select 然后按此名称排序,例如:
$query = $this->em
->createQuery("SELECT Month(p.planDate) as monthPlan, SUM(ROUND(p.ndata5/1000)) AS volume "
. "FROM NTPBundle:ParagonData p WHERE p.planDate >= :startDate AND p.ndata5<>0 "
. "GROUP BY monthPlan "
. "ORDER BY p.planDate")
->setParameter('startDate', $this->startDate->format('Y-m-d'));
感谢提供线索