--- includes/pager.inc	2006-10-15 14:57:05.000000000 -0500
+++ includes/pager.inc.new	2008-08-01 11:31:05.000000000 -0500
@@ -62,7 +62,12 @@ function pager_query($query, $limit = 10
 
   // Construct a count query if none was given.
   if (!isset($count_query)) {
-    $count_query = preg_replace(array('/SELECT.*?FROM /As', '/ORDER BY .*/'), array('SELECT COUNT(*) FROM ', ''), $query);
+    if (strpos($query, 'DISTINCT') > 0) {
+      $count_query = preg_replace(array('/SELECT.+DISTINCT?(.+)?\).+?FROM /As', '/ORDER BY .*/'), array('SELECT COUNT(DISTINCT\1)) FROM ', ''), $query);
+    }
+    else {
+      $count_query = preg_replace(array('/SELECT.*?FROM /As', '/ORDER BY .*/'), array('SELECT COUNT(*) FROM ', ''), $query);
+    }
   }
 
   // Convert comma-separated $page to an array, used by other functions.
