From 6a432c6a251da55aef6b83aacac757cb2d9a0e33 Mon Sep 17 00:00:00 2001 From: Molkobain Date: Thu, 6 Feb 2020 12:12:27 +0100 Subject: [PATCH] =?UTF-8?q?N=C2=B02757=20-=20Fix=20count=20in=20group=20by?= =?UTF-8?q?=20dashlets?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- core/sqlobjectquery.class.inc.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/core/sqlobjectquery.class.inc.php b/core/sqlobjectquery.class.inc.php index 38f767303..d7a59fcbb 100644 --- a/core/sqlobjectquery.class.inc.php +++ b/core/sqlobjectquery.class.inc.php @@ -457,7 +457,21 @@ class SQLObjectQuery extends SQLQuery { $sLimit = ''; } - $sSQL = "SELECT $sSelect,$sLineSep COUNT(*) AS _itop_count_$sLineSep FROM $sFrom$sLineSep WHERE $sWhere$sLineSep $sGroupBy $sOrderBy$sLineSep $sLimit"; + if (count($this->__aSelectedIdFields) > 0) + { + $aCountFields = array(); + foreach ($this->__aSelectedIdFields as $sFieldExpr) + { + $aCountFields[] = "COALESCE($sFieldExpr, 0)"; // Null values are excluded from the count + } + $sCountFields = implode(', ', $aCountFields); + $sCountClause = "DISTINCT $sCountFields"; + } + else + { + $sCountClause = '*'; + } + $sSQL = "SELECT $sSelect,$sLineSep COUNT($sCountClause) AS _itop_count_$sLineSep FROM $sFrom$sLineSep WHERE $sWhere$sLineSep $sGroupBy $sOrderBy$sLineSep $sLimit"; return $sSQL; }