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 6096543
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:56:22+00:00 2026-05-23T12:56:22+00:00

I am currently working on a problem that I have an inelegant solution for.

  • 0

I am currently working on a problem that I have an inelegant solution for. I am storing multiple versions of one record in a table. A good example would be the versions of a document. I have multiple entries for one document. I have a column called Code that stores an ID unique to the document. Each row also has an Active field (boolean) that indicates that the record is either “Active” (the latest version) or “Inactive”. In this case, only the latest version can be active. I am doing this to ease querying the data (instead of taking the top one of each document).

My data might look like this:

Code        Active    VersionNo
ABYY        0         1
RRUP        0         1
RRUP        0         2
ABYY        0         2
ABYY        0         3
ABYY        1         4
RRUP        1         3

My issue is that when I delete a record, I want to make sure that the document has one record (the latest one) marked as active. Here are three scenarios:

  1. I delete version 4 of document ABYY – I want to make version 3 of that document Active.
  2. I delete version 3 of document ABYY – I want to ensure that version 4 is still marked Active.
  3. (the tricky one) I delete version 3 of document ABYY. Later I decide to delete version 4. – I want version 2 to be marked Active, which means you can’t just subtract 1 from the version number.

Instead of creating a complicated query based off of specific data, I would rather create a generic query that set the Active fields to 0 for all documents except the one with the highest version number, which it would set to 1 instead. In this way, I could re-use the same query as a “clean-up” script if I was unsure of the data for a particular document.

Right now I’m stuck on two queries and the second is a bit long. Here is what I do:

UPDATE documents
SET Active = 0
WHERE Code = 'ABYY';

UPDATE documents u
SET Active = 1
WHERE id = (SELECT TOP 1 id
            FROM documents
            WHERE Code = 'ABYY'
            ORDER BY VersionNo DESC);

Could I possibly collapse these queries into one query (preferably simpler too)? I’m writing this for SQLite (latest version).

  • 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-23T12:56:23+00:00Added an answer on May 23, 2026 at 12:56 pm

    Try:

    UPDATE DOCUMENTS
       SET active = CASE 
                      WHEN id = (SELECT d.id
                                   FROM DOCUMENTS d
                                  WHERE d.code = DOCUMENTS.code
                               ORDER BY d.versionno DESC
                                  LIMIT 1) THEN 1
                      ELSE 0
                    END
     WHERE code = 'ABYY'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project I am currently working on that has multiple validators and
I have the following problem using subversion: I'm currently working on the trunk of
I am currently working on a MySQL query that contains a table: TBL:lesson_fee -fee_type_id
I'm currently working on camstudio_cl (here if interested) . The problem now is that
I'm currently working on two social networking sites that have a lot in common,
I'm currently working on a project that needs to have a process that assigns
Good morning, I am currently working on a project that utilises Codeigniter and the
I am currently working on a programming related problem where I am attempted to
Am currently working on an application that requires users to submit posts and comments
I`m currently working on a script, and I have the following situation. function somnicefunction()

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.