title: Duplicate resuts after annotation on django queryset
body: <p>I'm working with Django 1.10 with Django REST framework and need to create a filter on a relationship like this</p>
  
  <pre><code>class Player(models.Model):
      name = models.CharField(max_length=20, null=True, blank=True)
  
  class Score(models.Model):
      player = models.OneToOneField('companies.Company')
  
  class ScorePeriod(models.Model):
      score = models.ForeignKey('Score', on_delete=models.CASCADE)
      period = models.DateField()
      period_score = models.PositiveIntegerField()
  </code></pre>
  
  <p>The Player filter should filter by last period_score but only if the last period is before a given year. If the year is 2015 and there are ScorePeriod for 2016 and 2015 it should filter by the 2015 period_score. In case there are 2014 and 2013 ScorePeriod and the given year is 2015 should filter by 2014.</p>
  
  <p>In the views I'm using annotations so I can just filter by last_score in the filter file. This is the view.</p>
  
  <pre><code>queryset = super().get_queryset()
  max_year = get_starting_year()
  
  queryset = queryset.annotate(
      last_score_year_available=Max(Case(
          When(score__scoreperiod__period__year__lte=max_year,
               then=F('score__scoreperiod__period')),
          output_field=DateField()
      )),
  )
  
  queryset = queryset.annotate(
      last_score=Case(
          When(score__scoreperiod__period=F('last_score_year_available'),
               then=F('score__scoreperiod__period_score')),
          default=None,
          output_field=IntegerField(),
      ),
  )
  
  return queryset
  </code></pre>
  
  <p>This works as filter correctly but returns one duplicate for each ScorePeriod in the database. Also when getting something like "players/1" will fail because there are multiple objects in a get().</p>