Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3790700
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:21:19+00:00 2026-05-19T12:21:19+00:00

I have a sorting/grouping issue that I’m hoping somebody could add some insight on.

  • 0

I have a sorting/grouping issue that I’m hoping somebody could add some insight on.

We have a table of stories that have a publish date and an updated date. I’m using Django so it looks 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 the stories on a paginated page grouped by day. So…

Jan 20
    Story 1
    Story 2

Jan 19
    Story 1
    Story 3

The challenge is that if a story has an update_date it should be displayed twice, once on the pub_date day, and once on the update_day date (e.g. Story 1).

There are 10s of thousands of stories so I can’t do it all in python of course, but I don’t know of a way to do this query in SQL.

What I have right now is sorting everything by -pub_date and then getting a range of the max and min dates on a given page. I then query for any stories between those dates with an update_date and combine and group them in python. The problem is that the number of items on a page is irregular then.

So I guess my question is this: What is the best way to query a table for a list of items and sort them based on two fields, duplicating an item in the query if it has a value in the second field, and then sorting based on the two fields?

Hope that makes sense…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:21:20+00:00Added an answer on May 19, 2026 at 12:21 pm

    i can only think of “union” being able to do this.

    here’s an example of what that would look like. not sure how fast or good it is for the database to have this type of query sent to it often though D:

    the query assumes your table name is stories, and uses the columns headline, pub_date and update_date. it also assumes that a story that hasn’t been updated has the value null in the update_date column.

    SELECT      headline,
                the_date,
                DAY(the_date) AS the_day
    FROM (
        SELECT      headline,
                    pub_date AS the_date
        FROM        stories
        UNION
        SELECT      headline,
                    update_date AS the_date
        FROM        stories
        WHERE       update_date IS NOT NULL
    ) AS publishedandupdated
    ORDER BY    the_date DESC;
    

    if you want to add a limit to the query, it should be done last, after the “order by” clause.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WPF sorting/binding issue. (Disclaimer: I am very new to WPF and
I have a complex sorting problem with my SQL statement. I have a table
I have a problem with sorting items in table in PHP. Here is what
I have an issue of alphabetically sorting the contacts picked from the address book
I have a spreadsheet that I am sorting based on an item number column.
I've noticed that multi grouping in JQGrid elicits rows sorting to be rearranged inline
If I have a sorting algorithm, and I want to sort by some field
I have some difficulty sorting my array. It looks like this : [0] =>
I have an objects table and votes table. I am currently grouping them and
I have a CodeIgniter project that loads a table of data, an Account Name

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.