public function getCountSQL()
    {
        $statement = [];

        if ($this->distinct) {
            $distinct = 'DISTINCT ';
        } else {
            $distinct = '';
        }

        $statement[] = "(SELECT $distinct`" . $this->table . "`.*";
        $statement[] = $this->getFrom();
        $statement[] = $this->getJoin();
        $statement[] = $this->getWhere();
        $statement[] = $this->getOrder();
        $statement[] = ")";

        foreach ($this->unions as $union) {
            $statement[] = "UNION ".$distinct;
            $statement[] = $union->getCountSQL();
        }

        // Remove null values
        $statement = array_filter($statement);

        $sql = implode("\n", $statement);

        return $sql;
    }