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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:42:39+00:00 2026-05-25T10:42:39+00:00

Sometimes I want to join a record that I can easily identify as sort

  • 0

Sometimes I want to join a record that I can easily identify as “sort by this and that and pick the first record”. I wonder what the best practice here.

For example in my data model I have Services and Implementations. A Service may have multiple Implementations. Implementations have version numbers; zero or more implementations may be installed and one of them may also be enabled. Now I want to select the “current implementation” for each service using the following rules:

  • If nothing is installed, then the current implementation is the most recent one.
  • If more than one is installed, then the current is either the one that is enabled or, if nothing is enabled, the most recent one again.

Some examples:

Service  Version  Installed  Enabled
=======  =======  =========  =======
A        1        False      False
A        2        False      False   <- current for service A because most recent

B        1        True       False   <- current for service B because installed
B        2        False      False

C        1        True       False
C        2        True       False   <- current for service C because most recent
C        3        False      False      among installed

D        1        True       True    <- current for service D because enabled
D        2        True       False

I figured that if I sort by these fields and pick the first record, it will do the trick. I.e. I do something like that:

SELECT s.service, <other service fields>, i.version, <other impl. fields>
  FROM service s, implementation i
 WHERE i.rowid == (
       SELECT rowid 
         FROM implementation o
        WHERE o.service == s.service
     ORDER BY installed DESC, enabled DESC, version ASC)

Update. This one is rather specific to SQLite, I guess. First, rowid is the internal record ID that is used by SQLite. Second, in SQLite the SELECT expression in this context returns a single value. I guess it could be rewritten into a more generic SQL like this:

  ...
  FROM service s, implementation i
 WHERE i.service == s.service
   AND i.version == (
       SELECT version 
         FROM ...
     ORDER BY ...
        LIMIT 1)

It works here, but sometimes there’s no rowid or another single field that can serve as an identifier. Are there any other ways to get the same result? Something more generic, perhaps? (And for me something more specific is OK too, as long as it’s SQLite-specific 🙂

  • 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-25T10:42:40+00:00Added an answer on May 25, 2026 at 10:42 am

    You can use a NOT EXISTS condition to exclude all but the desired match. Join rows of [service] and [implementation] that match on the [service] column so long as there does not exist another row of [implementation] that also matches on the [service] column but is preferred because it would rank higher in the desired ordering. Here is the idea.

    SELECT s.service, <other service fields>, i.version, <other impl. fields>
    FROM service s JOIN implementation i
    ON i.service = s.service
    AND NOT EXISTS ( -- where there's no "better" row from i to use
    SELECT * FROM implementation AS i2
       WHERE i2.service = i.service
       AND (
         i2.installed > i.installed
         OR (i2.installed = i.installed AND i2.enabled > i.enabled)
         OR (i2.installed = i.installed AND i2.enabled = i.enabled AND i2.version < i.version)
       )
    )
    

    Using Microsoft SQL Server or another SQL dialect that supports the CROSS APPLY operator, this is much, much simpler:

    SELECT s.service, <other service fields>, i.version, <other impl. fields>
    FROM service s
    CROSS APPLY (
      SELECT TOP 1 * FROM implementation
      WHERE implementation.service = s.service
      ORDER BY installed DESC, enabled DESC, version ASC
    ) AS i
    

    (Neither solution has been tested with sample data, as CREATE TABLE and INSERT statements were not posted.)

    Steve Kass

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

Sidebar

Related Questions

I have a flash program that loads movie clips dynamically and sometimes they want
Windows's Snipping tool can capture the screen, but sometimes I want to capture the
Sometimes you want to mark a DB table record as deleted instead of deleting
This is my code: def execute(f, *args): f(args) I sometimes want to pass no
If I'm not online, I sometimes want to package some changes to a commit,
Sometimes I want to build Python or GCC from scratch just for fun, but
When using powershell, sometimes I want to only display for example the name or
As I learn new features in Oracle, sometimes I want to test how a
I have 2 classes both inherit from the same interface. i want sometimes to
sometimes when i want a winforms control to be docked also to the right

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.