code: def count(self):
          
          sql = u'SELECT count() FROM (%s)' % self.as_sql()
          raw = self._database.raw(sql)
          return int(raw) if raw else 0
docs: Returns the number of rows after aggregation.