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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:04:33+00:00 2026-05-13T10:04:33+00:00

I have a table with a field called sector, each sector is usually something

  • 0

I have a table with a field called sector, each sector is usually something like 1,2,3,4,5,6,7,etc.

I want to show available sectors in an application, I thought that showing all 1,2,3,4,5,6,7 is dumb so I should show “1 to 7” instead.

The problem is that sometimes the sectors skip one number like this 1,2,3, 5,6,7.
So I want to show something like 1 to 3, 5 to 7.

how could I query this in sql to show in my app?

  • 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-13T10:04:33+00:00Added an answer on May 13, 2026 at 10:04 am

    Some DBMS might have some OLAP functionality that makes it easy to write such queries, but IBM Informix Dynamic Server (IDS) does not yet have such functions.

    Let’s assume, for sake of concreteness, that your table is called ‘ProductSectors’ and has a structure like:

    CREATE TABLE ProductSectors
    (
        ProductID    INTEGER NOT NULL,
        Sector       INTEGER NOT NULL CHECK (Sector > 0),
        Name         VARCHAR(20) NOT NULL,
        PRIMARY KEY (ProductID, Sector)
    );
    

    What you are seeking within a particular ProductID is a list of the minimum and maximum contiguous values of Sector. A range is contiguous when there is no value one smaller than the minimum and no value one bigger than the maximum and there is no gap within the range. This is a complex query:

    SELECT P1.ProductID, P1.Sector AS Min_Sector, P2.Sector AS Max_Sector
      FROM ProductSectors P1 JOIN ProductSectors P2
        ON P1.ProductID = P2.ProductID
       AND P1.Sector   <= P2.Sector
     WHERE NOT EXISTS (SELECT *     -- no entry one smaller
                         FROM ProductSectors  P6
                        WHERE P1.ProductID  = P6.ProductID
                          AND P1.Sector - 1 = P6.Sector
                      )
       AND NOT EXISTS (SELECT *     -- no entry one larger
                         FROM ProductSectors  P5
                        WHERE P2.ProductID  = P5.ProductID
                          AND P2.Sector + 1 = P5.Sector
                      )
       AND NOT EXISTS (SELECT *     -- no gaps between P1.Sector and P2.Sector
                         FROM ProductSectors P3
                        WHERE P1.ProductID = P3.ProductID
                          AND P1.Sector   <= P3.Sector
                          AND P2.Sector   >  P3.Sector
                          AND NOT EXISTS (SELECT *
                                            FROM ProductSectors P4
                                           WHERE P4.ProductID = P3.ProductID
                                             AND P4.Sector    = P3.Sector + 1
                                         )
                      )
     ORDER BY P1.ProductID, Min_Sector;
    

    And here is a trace of the overall query working with sample data:

    CREATE TEMP TABLE productsectors
    (
        ProductID   INTEGER NOT NULL,
        Sector      INTEGER NOT NULL CHECK(Sector > 0),
        Name        VARCHAR(20),
        PRIMARY KEY (ProductID, Sector)
    );
    

    And some sample data, with various gaps:

    INSERT INTO ProductSectors VALUES(101, 1, "101:1");
    INSERT INTO ProductSectors VALUES(101, 2, "101:2");
    INSERT INTO ProductSectors VALUES(101, 3, "101:3");
    INSERT INTO ProductSectors VALUES(101, 4, "101:4");
    INSERT INTO ProductSectors VALUES(101, 5, "101:5");
    INSERT INTO ProductSectors VALUES(101, 6, "101:6");
    INSERT INTO ProductSectors VALUES(101, 7, "101:7");
    INSERT INTO ProductSectors VALUES(102, 1, "102:1");
    INSERT INTO ProductSectors VALUES(102, 2, "102:2");
    INSERT INTO ProductSectors VALUES(102, 4, "102:4");
    INSERT INTO ProductSectors VALUES(102, 5, "102:5");
    INSERT INTO ProductSectors VALUES(102, 6, "102:6");
    INSERT INTO ProductSectors VALUES(102, 7, "102:7");
    INSERT INTO ProductSectors VALUES(103, 1, "103:1");
    INSERT INTO ProductSectors VALUES(103, 2, "103:2");
    INSERT INTO ProductSectors VALUES(103, 4, "103:4");
    INSERT INTO ProductSectors VALUES(103, 6, "103:6");
    INSERT INTO ProductSectors VALUES(103, 7, "103:7");
    INSERT INTO ProductSectors VALUES(104, 1, "104:1");
    INSERT INTO ProductSectors VALUES(104, 2, "104:2");
    INSERT INTO ProductSectors VALUES(104, 3, "104:3");
    INSERT INTO ProductSectors VALUES(104, 6, "104:6");
    INSERT INTO ProductSectors VALUES(104, 7, "104:7");
    INSERT INTO ProductSectors VALUES(105, 1, "105:1");
    INSERT INTO ProductSectors VALUES(105, 4, "105:4");
    INSERT INTO ProductSectors VALUES(105, 5, "105:5");
    INSERT INTO ProductSectors VALUES(105, 7, "105:7");
    INSERT INTO ProductSectors VALUES(106, 1, "106:1");
    INSERT INTO ProductSectors VALUES(106, 2, "106:1");
    INSERT INTO ProductSectors VALUES(106, 3, "106:1");
    INSERT INTO ProductSectors VALUES(106, 7, "106:7");
    INSERT INTO ProductSectors VALUES(107, 7, "107:7");
    INSERT INTO ProductSectors VALUES(108, 8, "108:8");
    INSERT INTO ProductSectors VALUES(108, 9, "108:9");
    

    Required output – also the actual output:

    101|1|7 
    102|1|2 
    102|4|7 
    103|1|2 
    103|4|4 
    103|6|7 
    104|1|3 
    104|6|7 
    105|1|1 
    105|4|5 
    105|7|7 
    106|1|3 
    106|7|7 
    107|7|7 
    108|8|9 
    

    With the expected results on MacOS X 10.6.2, IDS 11.50.FC4W1, SQLCMD 86.04.

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

Sidebar

Ask A Question

Stats

  • Questions 284k
  • Answers 284k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I vagely remember encoutering something similar a while back, I… May 13, 2026 at 4:35 pm
  • Editorial Team
    Editorial Team added an answer It seems that there is a member called .ItemType for… May 13, 2026 at 4:35 pm
  • Editorial Team
    Editorial Team added an answer Constants created with define() can't be undefined once created. Constants… May 13, 2026 at 4:35 pm

Related Questions

I have a table with a field called 'user_car'. It consists of a cat'd
In DB I have a table with a field called fk_ownerID . By default,
So say I have a products table with a list of products, and one
I have a data object (let's say it's called 'Entry') that has a set
Current Implementation Sql Server 2005 Database with a table called messages with a column

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.