title: Sequelize Count and Group Nested Include
body: <p>I have got a few sql tables as</p>
  <pre><code>export default (sequelize, Sequelize) =&gt; {
  return sequelize.define('teacher', {
      tagline: {
          type: Sequelize.TEXT,
      },
      modeOfPayment: {
          type: Sequelize.STRING,
      },
      modeOfSession: {
          type: Sequelize.STRING,
      },
      preferredTimeZones: {
          type: Sequelize.STRING,
      },
      titleForSessions: {
          type: Sequelize.STRING,
      },
      availableForWork: {
          type: Sequelize.BOOLEAN,
      },
  });
  };
  
  export default (sequelize, Sequelize) =&gt; {
      return sequelize.define('skill', {
          name: {
              type: Sequelize.STRING,
              unique: true,
              allowNull: false,
          },
      });
  };
  
  export default (sequelize, Sequelize) =&gt; {
  return sequelize.define('category', {
      name: {
          type: Sequelize.STRING,
          unique: true,
          allowNull: false,
      },
  });
  };
  </code></pre>
  <p>Here is the model relation between them</p>
  <pre><code>Teacher.belongsToMany(Skill, {
      through: 'skill_teacher',
  });
  Skill.belongsToMany(Teacher, {
      through: 'skill_teacher',
  });
  
  Category.hasMany(Skill);
  Skill.belongsTo(Category);
  </code></pre>
  <p>Earlier I needed to query count of teachers in each skill, here's my controller for that</p>
  <pre><code>// @desc Get skill count for each skill
  // @route GET /api/skills/count
  // @access Public
  const getSkillCount = asyncHandler(async (req, res) =&gt; {
      try {
          const skills = await Skill.findAll({
              attributes: [
                  'id',
                  'name',
                  [sequelize.fn('count', sequelize.col('teachers.id')), 'teacherCount'],
              ],
              include: [{ attributes: [], model: Teacher }],
              group: ['skill.id'],
          });
          res.json(skills);
      } catch (err) {
          console.log(err.message);
          res.status(500);
          throw new Error(err.message);
      }
  });
  </code></pre>
  <p>Now, the skills are grouped in categories. So I wanna query a list of skills having their own teacher count grouped in their own categories, which category also having a skillCount column. I tried this but it is not giving my desired results</p>
  <pre><code>// @desc Get category and their skills counts
  // @route GET /api/categories/skills/count
  // @access Public
  const getCategorySkillCounts = asyncHandler(async (req, res) =&gt; {
      try {
          const categories = await Category.findAll({
              attributes: [
                  'id',
                  'name',
                  [sequelize.fn('count', sequelize.col('skills.id')), 'skillCount'],
              ],
              include: {
                  model: Skill,
                  include: [{ model: Teacher }],
                  attributes: [
                      'id',
                      'name',
                      [sequelize.fn('count', sequelize.col('teachers.id')), 'teacherCount'],
                  ],
                  group: ['skill.id'],
              },
              group: ['category.id'],
          });
  
          res.json(categories);
      } catch (err) {
          console.log(err.message);
          res.status(500);
          throw new Error(err.message);
      }
  });
  </code></pre>