Tuesday, October 26, 2010

Re: SELECT * FROM `student` WHERE mark=(select max(mark) from student)

On Oct 26, 10:24 am, Phlip <phlip2...@gmail.com> wrote:
> > This sounds like what django-reversion[1] does :)
>
> > [1]:http://github.com/etianen/django-reversion#readme
>
> We have to cover the situation where some clients might still have
> rev(n-1), while some are up-to-date with rev(n). So we _probably_ need
> the history in the same table as the current version.
>
> I'm aware this is borderline "big requirements up front", but the
> answer turns out to be...
>
> class ThingManager(models.Manager):
>
>     def get_query_set(self):
>         qs = super(ThingManager, self).get_query_set()
>         max_pids = QuerySet(self.model, using=self._db)
>         max_pids =
> max_pids.values('name').annotate(Max('pid')).values('pid')

Uh, that was

        max_pids =
max_pids.values('name').annotate(max_id=Max('pid')).values('pid')

If you don't name it you don't get the Max, even though nobody uses
the max_id in the resulting SELECT statement:

SELECT "things".*,
FROM "things"
WHERE ("things"."pid" IN (
SELECT MAX(U0."pid") AS "max_id"
FROM "things" U0
GROUP BY U0."name" ))


>         return qs.filter(pid__in=max_pids)
>
> Now we can write any ORM statement we can think of, and (if those
> lines continue to pass tests) then we only see the top horizon of the
> data. Unless we need to go deeper.
>
> Thanks, all!
>
> --
>   Phlip

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment