0

I'm trying to display number of orders made per customer in admin order grid, using the following join and select I could get the number or orders.

$collection->getSelect()->joinLeft('sales_flat_order', 'main_table.increment_id=sales_flat_order.increment_id','sales_flat_ordercustomer_email');
 $readAdapter = Mage::getSingleton('core/resource')
        ->getConnection('core_read');
     $select = $readAdapter->select()
        ->from('sales_flat_order') 
        ->columns(array('orders_count' => new Zend_Db_Expr('COUNT(*)')))
        ->group('sales_flat_order.customer_email');
                
                $collection->getSelect()
        ->joinLeft(  array('orders_count' => new Zend_Db_Expr('('.$select.')')),  'sales_flat_order.customer_email = orders_count.customer_email',  array('orders_count.orders_count')); 
        $collection->addFilterToMap('orders_count', 'orders_count.orders_count');

this is the result >>

increment_id  count_orders  email  
1000001       3             customer@customer.com  
1000008       3             customer@customer.com 
1000015       3             customer@customer.com

how could I get sum of orders at time of every order, not sum of all orders

increment_id  count_orders  email 
1000001       1             customer@customer.com 
1000008       2             customer@customer.com 
1000015       3             customer@customer.com
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.