| 587 | def test_subquery_row_range_rank(self): |
| 588 | qs = Employee.objects.annotate( |
| 589 | highest_avg_salary_date=Subquery( |
| 590 | Employee.objects.filter( |
| 591 | department=OuterRef('department'), |
| 592 | ).annotate( |
| 593 | avg_salary=Window( |
| 594 | expression=Avg('salary'), |
| 595 | order_by=[F('hire_date').asc()], |
| 596 | frame=RowRange(start=-1, end=1), |
| 597 | ), |
| 598 | ).order_by('-avg_salary', '-hire_date').values('hire_date')[:1], |
| 599 | ), |
| 600 | ).order_by('department', 'name') |
| 601 | self.assertQuerysetEqual(qs, [ |
| 602 | ('Adams', 'Accounting', datetime.date(2005, 11, 1)), |
| 603 | ('Jenson', 'Accounting', datetime.date(2005, 11, 1)), |
| 604 | ('Jones', 'Accounting', datetime.date(2005, 11, 1)), |
| 605 | ('Williams', 'Accounting', datetime.date(2005, 11, 1)), |
| 606 | ('Moore', 'IT', datetime.date(2013, 8, 1)), |
| 607 | ('Wilkinson', 'IT', datetime.date(2013, 8, 1)), |
| 608 | ('Johnson', 'Management', datetime.date(2005, 7, 1)), |
| 609 | ('Miller', 'Management', datetime.date(2005, 7, 1)), |
| 610 | ('Johnson', 'Marketing', datetime.date(2012, 3, 1)), |
| 611 | ('Smith', 'Marketing', datetime.date(2012, 3, 1)), |
| 612 | ('Brown', 'Sales', datetime.date(2009, 9, 1)), |
| 613 | ('Smith', 'Sales', datetime.date(2009, 9, 1)), |
| 614 | ], transform=lambda row: (row.name, row.department, row.highest_avg_salary_date)) |
| 615 | |