Title self esplicative, but when I perform the join, then add field to select then make the expression I get an error.
$this->getCollection()->getSelect()
->joinLeft(
[
"transaction" => $this->getCollection()->getTable("transaction_table")
],
"(transaction.supplier_id = main_table.supplier_id)",
[
'transaction_id' => 'entity_id',
'amount' => 'amount',
'date' => 'date'
]
)
->joinInner(
[
'supplier' => $this->getCollection()->getTable('supplier_table')
],
"(supplier.entity_id = main_table.supplier_id)",
[
'business_name' => 'business_name'
]
);
$this->getCollection()
->addFieldToSelect(new Zend_Db_Expr("CASE WHEN amount is NULL THEN 0 ELSE amount END AS amount_sum"))
->addFieldToSelect(new Zend_Db_Expr("CASE WHEN date is NULL THEN main_table.start_date ELSE date END AS date"));
$this->getCollection()->getSelect()
->columns(['current_amount' => new Zend_Db_Expr('SUM(amount_sum)')])
->group('main_table.entity_id');
But I get the following error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'amount_sum' in 'field list', query was: SELECT CASE WHEN amount is NULL THEN 0 ELSE amount END AS amount_sum, CASE WHEN date is NULL THEN main_table.start_date ELSE date END AS date, `transaction`.`entity_id` AS `transaction_id`, `transaction`.`amount`, `transaction`.`date`, `supplier`.`business_name`, SUM(amount_sum) AS `current_amount` FROM `objective_table` AS `main_table` LEFT JOIN `transaction_table` AS `transaction` ON (transaction.supplier_id = main_table.supplier_id) INNER JOIN `supplier_table` AS `supplier` ON (supplier.entity_id = main_table.supplier_id) GROUP BY `main_table`.`entity_id` LIMIT 20
Suggestions? I’m going crazy