title: How to count an include inside a another include sequelize
body: <p>I have 3 tables, and i need to count how much Cupons my table orders have, my table Cupons belongs to my table orders, and my table orders belong to my table Afiliado, i need to fetch all 'Afiliates', and get all Orders and all Cupons of the order and count the Cupons, i tried this way but doesn't work</p>
  <pre><code>var Afiliadosorders = afiliados.map(async (item) =&gt; {
              return await Afiliados.findByPk(item.id, {
                  include: {
                      model: Orders,
                      as: 'Ordens',
                      attributes: {
                          include: [
                              'order_id'
                              [Sequelize.fn(&quot;COUNT&quot;, Sequelize.col(&quot;Cupons.id&quot;)), &quot;CuponsCount&quot;]
                          ],
                          separate:true,
                          limit: 1
                      },
                      include: {
                          model: Cupom,
                          as: 'Cupons',
                      },
                  },
              })
          })
  </code></pre>
  <p>@EDIT
  It's solved with the @Anatoly comment, with subquery, the finally solution if somebody else have the same issue</p>
  <pre><code>var Afiliadosorders = afiliados.map(async (item) =&gt; {
              return await Afiliados.findByPk(item.id, {
                  include: {
                      model: Orders,
                      as: 'Ordens',
                      required: true,
                      attributes: {
                          include: [
                              [Sequelize.literal('(SELECT COUNT(*) FROM cupom where cupom.order_id = &quot;Ordens&quot;.&quot;id&quot;)'), &quot;Total_Cupons&quot;]
                          ],
                      }
                  },
              })
          })
      ````
  </code></pre>