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

  • Home
  • SEARCH
  • 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 5849993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T13:04:44+00:00 2026-05-22T13:04:44+00:00

I have a question about a particular query I’d like to execute against a

  • 0

I have a question about a particular query I’d like to execute against a PostgreSQL table. Although I welcome criticism of the table scheme I’ve used I’m going to be more appreciative of answers to my actual question!

I’m using the uuid-ossp postgresql-contrib module and have the following table structure:

       Column        |            Type             | Modifiers | Storage  | Description
---------------------+-----------------------------+-----------+----------+-------------
 revision_id         | uuid                        | not null  | plain    |
 document_id         | uuid                        | not null  | plain    |
 user_id             | uuid                        | not null  | plain    |
 datetime_edited     | timestamp without time zone | not null  | plain    |
 contents            | text                        | not null  | extended |
Indexes:
    "document_pkey" PRIMARY KEY, btree (revision_id)

The idea is that:

  • A document may have one or more revisions. Revisions are not deleted. In order to update a document a new row is inserted with a new revision_id but an identical document_id.
  • revision_id is unique across all revisions for all documents.
  • contents is a blob of data that represents the document, and user_id identifies who updated the document.

I’m struggling to come up with a query that returns all the latest revisions for all documents created by a particular user. I know I can do, for example:

select * from document where user_id = '6a2aabc417b34ef99b14b10eaa8e9313';

but this returns all the documents. How do I drill down and ask for a grouping by document_id, and also LIMIT 1 and return the newest revision_id based on datetime_edited?

EDIT: Since a document can have one or more revisions I’ve been far to vague in saying “all documents created by a user”. By created I mean that the user has contributed one or more revisions to the documents, i.e. there is at least one revision where the user edited the document.

Is something like this even achievable in one query, or do I need to hit the database several times to achieve this?

EDIT: revision_id is not monotonically increasing. It’s a random UUID. Hence, max(revision_id) != max(datetime_edited).

  • 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-22T13:04:44+00:00Added an answer on May 22, 2026 at 1:04 pm
    Select ...
    From document As D
        Join    (
                Select D1.document_id, Max( datetime_edited ) As datetime_edited
                From document As D1
                Group By D1.document_id
                ) As LastRevision
            On LastRevision.document_id = D.document_id
                And LastRevision.datetime_edited = D.datetime_edited
    Where Exists    (
                    Select 1
                    From document As D2
                    Where D2.document_id = D.document_id
                        And D2.user_id = '6a2aabc417b34ef99b14b10eaa8e9313'
                    )
    

    An alternate form:

    Select ...
    From document As D
        Join    (
                Select D1.document_id, Max( datetime_edited ) As datetime_edited
                From document As D1
                Group By D1.document_id
                ) As LastRevision
            On LastRevision.document_id = D.document_id
                And LastRevision.datetime_edited = D.datetime_edited
        Join    (
                Select D2.document_id
                From document As D2
                Where D2.user_id = '6a2aabc417b34ef99b14b10eaa8e9313'
                Group By D2.document_id
                ) As UserDocs
            On UserDocs.document_id = D.document_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to ask something about query using mysql I have this table
I have some questions about multi-threaded programming and multi-core usage. In particular I'm wondering
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have a question about using streams in .NET to load files from disk.
I have a question about best practices regarding how one should approach storing complex
I have a question about locking. This doesn't have to be only about record
I have a question about how to deploy WPF application into a PC without
I have a question about using os.execvp in Python. I have the following bit
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
I have a question about this question . I posted a reply there but

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.