1

How would I apply this query to Magento to display results on page. Basically I want to show the top 10 users total sales purchase amount

SELECT customer_id,SUM(base_grand_total) AS total_sales 
FROM sales_flat_order
GROUP BY customer_id
ORDER BY total_sales DESC
LIMIT 10;
asked Oct 13, 2014 at 18:18

2 Answers 2

1

Try

$_collection = Mage::getModel('sales/order')->getCollection();
$_collection->getSelect()
 ->reset(Zend_Db_Select::COLUMNS)
 ->columns('customer_id')
 ->columns('SUM(base_grand_total) AS total_sales')
 ->order('total_sales')
 ->group('customer_id')
 ->limit(10);

or

/**
 * Get the resource model
 */
$resource = Mage::getSingleton('core/resource');
/**
 * Retrieve the read connection
 */
$readConnection = $resource->getConnection('core_read');
$query = 'SELECT customer_id,SUM(base_grand_total) AS total_sales 
 FROM sales_flat_order
 GROUP BY customer_id
 ORDER BY total_sales DESC
 LIMIT 10;';
/**
 * Execute the query and store the results in $results
 */
$results = $readConnection->fetchAll($query);

See https://stackoverflow.com/questions/5790811/aggregate-functions-in-magento-orm

answered Oct 13, 2014 at 19:41
1

Here is a quick little tutorial over a module called Clean SQL Reports. It lets you do arbitrary SQL reports in the magento admin section.

goivvy.com
4,24325 silver badges38 bronze badges
answered Oct 13, 2014 at 19:57

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.