On Fri, Apr 6, 2012 at 10:58 PM, Dennis Lee Bieber <[email protected]>wrote:
> On Fri, 6 Apr 2012 22:34:35 -0700, Mark Phillips > <[email protected]> declaimed the following in > gmane.comp.python.django.user: > > > > > Basically, I have sets of data determined by a foreign key, and I want to > > know the order in which the rows arrive within a data set. > > > IOWs, you don't really need a value incrementing per foreign key... > After all, the primary key already reflects the /order/ of new data > inserts. > > In plain SQL, this might be an application for a "group by" the > foreign key, "order by" the primary key. Note that your example is NOT > very clear. "Reset to zero" could result in: > > 1 1 1 .... > 2 1 2 ... > 3 2 1 ... > 4 1 1 ... > 5 2 1 ... > 6 2 2 ... > > I don't understand what you mean by "reset to zero". > Whereas, ignoring the example "sequence" column, group by/order by > would return > > 1 1 1 ... > 2 1 2 ... > 4 1 1 ... > 3 2 1 ... > 5 2 1 ... > 6 2 2 ... > > Actually, just an ORDER BY FK, PK would produce the above... GROUP > BY would be used if you need something like counts/avg for each FK set. > I agree that if the data in the db was entered correctly in the first place, then just ORDER BY on the FK and PK would be sufficient. However, if the user needs to insert some data between sequence #3 and sequence #4, then I have to muck with the primary key of the table, which sounds dangerous. Perhaps a better analogy would be to think of a linked list as my in memory data model, and I want to translate that to a sql table(s). I found a reference http://www.artfulsoftware.com/mysqlbook/sampler/mysqled1ch20.html, that may solve my problem. Apologies for not being clearer in my original post. Mark -- You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

