Magento: Wrong count in admin Grid when using GROUP BY clause, overriding lib module

If you have noticed or not, in version 1.5 of Magento when you use GROUP BY clause in any Grid.php file in admin, the count always display wrong. Many times it displays 1 even if there are hundreds of records. Due to this your pagination also doesn’t work. This is a bug in Magento. Your getSize() always returns wrong count whereas total records in grid are proper.

To fix this, you need to edit one of your core file. As it’s not a good practice to edit core file, we will here override the core file.
Overriding LIB module

Copy Db.php file from magento / lib / Varien / Data / Collection / Db.php
Paste it to your local directory so the resultant folder structure would look like this:
magento / app / code / local / Varien / Data / Collection / Db.php

Now open this file to edit and replace getSelectCountSql function with below one

public function getSelectCountSql()

$countSelect = clone $this->getSelect();

if(count($this->getSelect()->getPart(Zend_Db_Select::GROUP)) > 0) {
$group = $this->getSelect()->getPart(Zend_Db_Select::GROUP);
$countSelect->columns("COUNT(DISTINCT ".implode(", ", $group).")");
} else {
return $countSelect;

Updated the method for generic use

Thanks for reading my blog. I hope it will help you, Please leave your comments here if it’s really help you. 🙂  

Thank You :),
Mayank Zalavadia