}
$select = implode(', ', $select);
- $orderBy = 'ORDER BY j.start_date DESC';
+ $orderBy = 'ORDER BY MIN(j.start_date) DESC';
if ($sort) {
$orderBy = "ORDER BY $sort";
}
+ $groupBy = CRM_Contact_BAO_Query::getGroupByFromSelectColumns(array_keys($fields), "m.id");
+
$sql = "
SELECT $select
FROM civicrm_mailing m
WHERE j.is_test = 0
AND meq.contact_id = %1
$whereClause
-GROUP BY m.id
+{$groupBy}
{$orderBy}
";