I have a sorting / grouping problem, and I hope someone can add some information.
We have a table of stories in which there is a publication date and an updated date. I use Django to make it look like this:
class Story(models.Model): pub_date = models.DateTimeField(db_index=True) update_date = models.DateTimeField(blank=True, null=True, db_index=True) headline = models.CharField(max_length=200) ...
We want to display stories on a page by page, grouped by day. So that...
Jan 20 Story 1 Story 2 Jan 19 Story 1 Story 3
The problem is that if the story has update_date, it should be displayed twice, once a day pub_date and once on the date update_day (for example, Story 1).
There are 10 thousand thousand stories, so of course I canβt do all this in python, but I donβt know how to do it in SQL.
Now I have to sort everything on -pub_date, and then get the range of maximum and minimum dates on this page. Then I query any stories between these dates with update_date and merge and group them in python. The problem is that the number of elements on the page is irregular.
So, I think my question is this: what is the best way to query a table for a list of elements and sort them based on two fields, duplicate an element in a query if it matters in the second field and then sort based on two fields?
Hope this makes sense ...
source share