title: How to insert a Distinct and Order in include?
body: <p>i'm doing a project in nodejs (express and angular) and im finding difficulties with this sequielize. I cannot add a distinct (at the beginning of the query, i receive too much copies) and i cannot add an order by for a "include field"</p>
  
  <p>I went through Sequielize documentation ( <a href="http://docs.sequelizejs.com/manual/querying.html#operators-aliases" rel="nofollow noreferrer">http://docs.sequelizejs.com/manual/querying.html#operators-aliases</a> ) but without success. I found some answers in here but still no success. What am I doing wrong?</p>
  
  <pre><code>    return models.Inspection.findAndCountAll({
          distinct: true,
          attributes: [],
          include: [{model: models.InspectionGroup, attributes:['ID', 'InspectionTypeModel_ID', 'Notes', 'Date'], 
                              required: true, include: [{
                                  model: models.InspectionTypeModel, attributes: ['Model'], required: true
                              }]},
                      {model: models.UnitElement, attributes: [], required: true, include: [{
                          model: models.Span, attributes: [], required: true, include: [{
                              model: models.StructuralUnit, attributes: [], required: true, include: [{
                                  model: models.Building, attributes: [], required: true, include: [{
                                      model: models.Direction, attributes: [], required: true, include: [{
                                          model: models.Road, attributes: [], where: {Dealer_ID: dealerList}, required: true
                                      }]
                                  }]
                              }]
                          }]
                      }]
                  }],
      order: [[ { model: models.InspectionGroup }, 'Date', 'DESC']],
      limit: limit,
      offset: offset
      })
      .then(function (results) {
          res.json(results);
      })
      .catch(function (err) {
          commons.showerror(err, res);
      });
  </code></pre>
  
  <p>This is the sql generated: <br></p>
  
  <pre><code>SELECT [Inspection].[ID], [InspectionGroup].[ID] AS [InspectionGroup.ID], [InspectionGroup].[InspectionTypeModel_ID] AS [InspectionGroup.InspectionTypeModel_ID], 
      [InspectionGroup].[Notes] AS [InspectionGroup.Notes], [InspectionGroup].[Date] AS [InspectionGroup.Date], [InspectionGroup.InspectionTypeModel].[ID] AS [InspectionGroup.InspectionTypeModel.ID], 
      [InspectionGroup.InspectionTypeModel].[Model] AS [InspectionGroup.InspectionTypeModel.Model] 
  FROM [Inspection] AS [Inspection] 
  INNER JOIN [InspectionGroup] AS [InspectionGroup] ON [Inspection].[InspectionGroup_ID] = [InspectionGroup].[ID] 
  INNER JOIN [InspectionTypeModel] AS [InspectionGroup.InspectionTypeModel] ON [InspectionGroup].[InspectionTypeModel_ID] = [InspectionGroup.InspectionTypeModel].[ID] 
  INNER JOIN [UnitElement] AS [UnitElement] ON [Inspection].[UnitElement_ID] = [UnitElement].[ID] INNER JOIN [Span] AS [UnitElement.Span] ON [UnitElement].[Span_ID] = [UnitElement.Span].[ID] 
  INNER JOIN [StructuralUnit] AS [UnitElement.Span.StructuralUnit] ON [UnitElement.Span].[StructuralUnit_ID] = [UnitElement.Span.StructuralUnit].[ID] INNER JOIN [Building] AS [UnitElement.Span.StructuralUnit.Building] ON [UnitElement.Span.StructuralUnit].[Building_ID] = [UnitElement.Span.StructuralUnit.Building].[ID] 
  INNER JOIN [Direction] AS [UnitElement.Span.StructuralUnit.Building.Direction] ON [UnitElement.Span.StructuralUnit.Building].[Direction_ID] = [UnitElement.Span.StructuralUnit.Building.Direction].[ID] INNER JOIN [Road] AS [UnitElement.Span.StructuralUnit.Building.Direction.Road] ON [UnitElement.Span.StructuralUnit.Building.Direction].[Road_ID] = [UnitElement.Span.StructuralUnit.Building.Direction.Road].[ID] AND [UnitElement.Span.StructuralUnit.Building.Direction.Road].[Dealer_ID] IN (4, 12) 
  ORDER BY [InspectionGroup].[Date] DESC ORDER BY [ID] OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY
  </code></pre>
  
  <p>And this is the error <br>
  Invalid usage of the option NEXT in the FETCH statement.</p>
  
  <p>I can see that the sequielize generated 2 different order by, but I actually don't know why it generates the "ID order by", considering i specified that the order by should be on InspectionGroup.Date...and the "distinct:true" is totally ignored.</p>
  
  <p>Edit (generated SQL with attributes: [[Sequelize.fn('distinct', Sequelize.col('ID')), 'inspectionId']])
  And error: "Invalid usage of the option NEXT in the FETCH statement."</p>
  
  <pre><code>SELECT [Inspection].[ID], distinct([ID]) AS [inspectionId], [InspectionGroup].[ID] AS [InspectionGroup.ID], [InspectionGroup].[InspectionTypeModel_ID] AS [InspectionGroup.InspectionTypeModel_ID], 
      [InspectionGroup].[Notes] AS [InspectionGroup.Notes], [InspectionGroup].[Date] AS [InspectionGroup.Date], [InspectionGroup.InspectionTypeModel].[ID] AS [InspectionGroup.InspectionTypeModel.ID], 
      [InspectionGroup.InspectionTypeModel].[Model] AS [InspectionGroup.InspectionTypeModel.Model]
      FROM [Inspection] AS [Inspection] 
      INNER JOIN [InspectionGroup] AS [InspectionGroup] ON [Inspection].        [InspectionGroup_ID] = [InspectionGroup].[ID] 
      INNER JOIN [InspectionTypeModel] AS [InspectionGroup.InspectionTypeModel] ON [InspectionGroup].[InspectionTypeModel_ID] = [InspectionGroup.InspectionTypeModel].[ID] 
      INNER JOIN [UnitElement] AS [UnitElement] ON [Inspection].[UnitElement_ID] = [UnitElement].[ID] INNER JOIN [Span] AS [UnitElement.Span] ON [UnitElement].[Span_ID] = [UnitElement.Span].[ID] 
      INNER JOIN [StructuralUnit] AS [UnitElement.Span.StructuralUnit] ON [UnitElement.Span].[StructuralUnit_ID] = [UnitElement.Span.StructuralUnit].[ID] INNER JOIN [Building] AS [UnitElement.Span.StructuralUnit.Building] ON [UnitElement.Span.StructuralUnit].[Building_ID] = [UnitElement.Span.StructuralUnit.Building].[ID] 
      INNER JOIN [Direction] AS [UnitElement.Span.StructuralUnit.Building.Direction] ON [UnitElement.Span.StructuralUnit.Building].[Direction_ID] = [UnitElement.Span.StructuralUnit.Building.Direction].[ID] 
      INNER JOIN [Road] AS [UnitElement.Span.StructuralUnit.Building.Direction.Road] ON [UnitElement.Span.StructuralUnit.Building.Direction].[Road_ID] = [UnitElement.Span.StructuralUnit.Building.Direction.Road].[ID] AND [UnitElement.Span.StructuralUnit.Building.Direction.Road].[Dealer_ID] IN (4, 12) 
      ORDER BY [Inspection].[Date] DESC OFFSET 0 ROWS FETCH NEXT 10 ROWS ONLY
  </code></pre>