Skip to content

Instantly share code, notes, and snippets.

@euperia
Created August 17, 2016 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save euperia/d17df77ff5f7cad5dd87fd61bccf36d3 to your computer and use it in GitHub Desktop.
Save euperia/d17df77ff5f7cad5dd87fd61bccf36d3 to your computer and use it in GitHub Desktop.
How to use Group By on a Magento Collection
<?php
$collection = Mage::getModel('sales/order_item')->getCollection();
$collection
->addAttributeToFilter('updated_at', array('gt' => $this->reportStartDate))
->addAttributeToFilter('updated_at', array('lt' => $this->reportEndDate));
// this is where we add the group by
$collection->getSelect()->group('main_table.sku');
$collection->setOrder('updated_at', 'DESC');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment