Hello all,

I know people have asked about custom aggregates/annotations before, but
 I haven't quite been able to figure out how to translate those
discussions into what I need (or even if a custom aggregate is the way
to go).  Any advice would be much appreciated.

What I would like to do is sequentially number the rows in a query,
within subgroups, as follows.  Suppose I have three columns, subject_id,
experiment_id, and date, and the query is ordered by those columns
respectively (a la queryset.order('subject__id', 'experiment__id',
'date')).  I would like each record in the result to be annotated with a
session_number that describes that record's sequence within the set of
records with the same experiment_id and subject_id.  Here is an example
of what I mean:

| subject_id | experiment_id | date    | session_number |
---------------------------------------------------------
|     1      |       4       | 5/27/09 |        0       |
|     1      |       4       | 5/28/09 |        1       |
|     1      |       4       | 5/29/09 |        2       |
---------------------------------------------------------
|     1      |       5       | 3/2/09  |        0       |
|     1      |       5       | 3/5/09  |        1       |
---------------------------------------------------------
|     2      |       4       | 5/22/09 |        0       |
|     2      |       4       | 5/23/09 |        1       |

and so on.

I confess I'm a bit lost as to where to begin with this.  It seems like
the kind of thing that a relational database should be exceedingly good
at, so I would hope that a custom annotation wouldn't be hard to come up
 with, but I don't know much about the ins and outs of SQL functions or
the right way to calculate what I want.

Thanks for any help you can provide!

Best,
Richard

--~--~---------~--~----~------------~-------~--~----~
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+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to