How can I rewrite this query to Doctrine DBAL query or better to createQuery() structure? When I used createQuery() "data_last" was not recognized as date but as string and generated errors. Maybe better solution would be internal left join and calculation done on symfony side? ROUND function is not recognised by Doctrine.
$sql='SELECT
q.fund_id AS tfi,
f.fundName AS name,
( SELECT `date`
FROM quotes
WHERE DATE_SUB( "'. $sToday .'", INTERVAL 1 MONTH ) <= `date` AND `fund_id` = `tfi`
ORDER BY `date` DESC
LIMIT 1) AS date_last,
( SELECT `value`
FROM quotes
WHERE `fund_id` = `tfi` AND `date` = `date_last`
LIMIT 1) AS value_last,
( SELECT `date`
FROM quotes
WHERE DATE_SUB( date_last, INTERVAL 1 MONTH ) <= `date` AND `fund_id` = `tfi`
ORDER BY `date` ASC
LIMIT 1) AS date_1m,
( SELECT `value`
FROM quotes
WHERE `fund_id` = `tfi` AND `date` = `date_1m`
LIMIT 1) AS value_1m,
( SELECT ROUND( (value_last-value_1m)/value_1m*100, 2) ) AS chg_1m
FROM quotes q
LEFT JOIN funds f
ON ( q.fund_id = f.id )
GROUP BY q.fund_id
ORDER BY f.fundName';
Whoa, I wouldn't waste any time trying to rewrite query this complexity. You're definitely better off with Doctrine Native SQL