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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:14:30+00:00 2026-05-12T16:14:30+00:00

I have a bit of an SQL problem. Here are my tables: areas(id, name,

  • 0

I have a bit of an SQL problem. Here are my tables:

areas(id, name, sla_id)
areas_groups(id, group_id, areaprefix)

The sla_id is an identifier from a different source – it is unique, but areas has its own auto-incrementing primary key.

The areaprefix field is the interesting one. It just contains the first few digits of the sla_id and is unique. Each area can only exist in one group, so the area belongs to the group with the most specific prefix. Example:

Group 12's area prefixes: 105, 110, 115, 805
Group 13's area prefixes: 1, 8

Area sla_id = 10533071 matches both group 12 (105*) and group 13 (1*)
              "105" is longer, so this area is in group 12
Area sla_id = 81031983 matches only group 13 (8*)

The reason it’s done like this is so we can easily make a “catch-all” group for areas which don’t fall into any other group.

I can find which group an area is in like this:

-- eg: area with sla_id 105055200
SELECT * FROM (
    SELECT group_id
    FROM areas_groups
    WHERE SUBSTR('105055200', 0, LENGTH(area_prefix)) = area_prefix
    ORDER BY LENGTH(area_prefix) DESC
)
WHERE rownum = 1;

(Did I mention this is Oracle?)

Going the other way is the tricky one: Given a group Id, I want to find all the areas which belong to that group. That is, given group 13, I want all the areas that start with 1 or 8 but not 105, 110, 115 or 805 (in this example).

The closest I’ve come is this:

SELECT a.id, a.sla_id, MAX(LENGTH(ag.area_prefix)), ag.group_id
FROM areas a INNER JOIN areas_groups ag
    ON (SUBSTR(a.sla_id, 0, LENGTH(ag.area_prefix)) = ag.area_prefix)
WHERE a.sla_id IS NOT NULL
GROUP BY a.id, a.sla_id, ag.group_id

That returns data like this:

 id     sla_id    leng   group_id
583    105308400    3    12
583    105308400    1    13
584    105556700    3    12
584    105556700    1    13

So if I could only grab the group_id which has the longest length for each id… I have a feeling that I’m really close but just missing a tiny little thing… Can anyone help put me out of my misery?

  • 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-12T16:14:31+00:00Added an answer on May 12, 2026 at 4:14 pm
    select id
    ,      sla_id
    ,      leng
    ,      group_id
    ,      (row_number() over (partition by id order by leng desc)) rn 
    from 
    (
    SELECT a.id, a.sla_id, MAX(LENGTH(ag.area_prefix)) leng, ag.group_id
    FROM areas a INNER JOIN areas_groups ag
        ON (SUBSTR(a.sla_id, 0, LENGTH(ag.area_prefix)) = ag.area_prefix)
    WHERE a.sla_id IS NOT NULL
    GROUP BY a.id, a.sla_id, ag.group_id
    )
    where rn = 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that is currently running against a 32-bit SQL Server 2005
I have an SQL Server DB with a table with these fields: A bit
I have started using Linq to SQL in a (bit DDD like) system which
I'm having a bit of a problem with moving specific data from one server
I have this bit of javascript written with jQuery 1.2.5. It's contained inside the
I have a bit of code that basically reads an XML document using the
I have a bit of code that passes around a ton of objects and
I have a bit of a hybrid situation on my hands. I'm writing an
I have a bit of html like so: <a href=#somthing id=a1><img src=something /></a> <a
I have this bit of script to widen a text box on mouseover and

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.