Search code examples
phpsqlsymfonydql

DQL/SQL GROUP BY MONTH()


Is it possible to GROUP BY month having only date as a column? This can be achieved by PHP loop but I would prefer to use SQL. Live code below:

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

Solution

  • Right sort it you have to name select then ORDER BY this name like:

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

    thanks for clues