I am trying to convert this, I think, a simple mysql query to a Doctrine dql, however Im now having a pretty serious fight ...
SELECT (c.prix-aggregates.AVG) AS test FROM immobilier_ad_blank c CROSS JOIN ( SELECT AVG(prix) AS AVG FROM immobilier_ad_blank) AS aggregates
The purpose of this: creating a z-score. Original implementation coming from this question. Calculation of Z-Score for each row in MySQL? (plain)
I was thinking about creating an association within an entity, but I mean it is not necessary, it is only for statistics.
Edit: Btw, I don't want to use raw SQL, I will extract the βsubqueryβ from another query builder expression using getDQL . Otherwise, I will have to rewrite my dynamic query builder to take into account rawSQL.
Edit 2: Tried this
$subQb = $this->_em->createQueryBuilder(); $subQb->addSelect("AVG(subC.prix) as AMEAN") ->from("MomoaIntegrationBundle:sources\Common", "subC"); $subDql = $subQb->getDQL(); $dql = "SELECT c.prix FROM MomoaIntegrationBundle:sources\Common c INNER JOIN ($subDql) AS aggregates";
Raw dql:
SELECT c.prix FROM MomoaIntegrationBundle:sources\Common c INNER JOIN (SELECT AVG(subC.prix) as AMEAN FROM MomoaIntegrationBundle:sources\Common subC) AS aggregates
Getting this strange error: line 0, col 70 near '(SELECT AVG(subC.prix)': Error: Class '(' is not defined.
Edit 3: I found a kind of furious way to make it work, but the teaching is stubborn with its implementation of entities, etc., And I forgot that STATISTICS ARE NECESSARY!
$subQb = $this->_em->createQueryBuilder(); $subQb->addSelect("AVG(subC.prix) as AMEAN") ->from("MomoaIntegrationBundle:sources\Common", "subC"); $sql = "SELECT (c.prix-aggregates.sclr_0) AS test FROM immobilier_ad_blank c CROSS JOIN " . "({$subQb->getQuery()->getSQL()}) AS aggregates"; $stm = $stm = $this->_em->getConnection()->prepare($sql); $stm->execute(); $data = $stm->fetchAll();
If you have a better solution, Im all ears! I really don't like this solution.