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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:28:31+00:00 2026-05-16T16:28:31+00:00

I have a problem in hand and that is to fetch a result set

  • 0

I have a problem in hand and that is to fetch a result set in a numeric pattern / sequence based on a single column’s value.

Say I have the following table:

+-------+  
| val   |  
+-------+
| 1     |
| 1     |
| 1     |
| 1     |
| 2     |
| 2     |
| 2     |
| 2     |
| 3     |
| 3     |
| 3     |
| 3     |
+-------+

How can I make it this way instead:

+-------+  
| val   |  
+-------+
| 1     |
| 2     |
| 3     |
| 1     |
| 2     |
| 3     |
| 1     |
| 2     |
| 3     |
| 1     |
| 2     |
| 3     |
+-------+

Case mentioned by aularon:

What if one of the values do not have enough to fill the gaps, what is the expected behaviour? Say we replace two of the 3’s to be 8’s

+-------+  
| val   |  
+-------+
| 1     |
| 1     |
| 1     |
| 1     |
| 2     |
| 2     |
| 2     |
| 2     |
| 3     |
| 3     |
| 8     |
| 8     |
+-------+

I’d still expect it to go in numeric sequence of smallest first, then largest, then smallest, then largest.

+-------+  
| val   |  
+-------+
| 1     |
| 2     |
| 3     |
| 8     |
| 1     |
| 2     |
| 3     |
| 8     |
| 1     |
| 2     |
| 1     |
| 2     |
+-------+
  • 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-16T16:28:32+00:00Added an answer on May 16, 2026 at 4:28 pm

    This is what we call a major rownum hack. Let’s say your table is named t and your value column is named val. This will give the result you’re looking for.

    SELECT val    /* step 6 */
    FROM
    (SELECT A.val, 
       CASE 
       WHEN A.val <> IFNULL(B.val,-1) THEN @q:=0   /* step 4 */
       ELSE @q:=@q+1                               /* step 4 */ 
        END AS seq                                 /* step 4 */
       FROM
        (SELECT @q:=0)r,
        (SELECT                          /* step 1 */
                @a:=@a+1 AS n,           /* step 1 */
                val                      /* step 1 */
          FROM  (SELECT @a:= 0)r, T      /* step 1 */
         ORDER BY val) A                 /* step 1 */
       LEFT JOIN
        (SELECT                        /* step 2, like step 1 */
                @b:=@b+1 AS n, 
                val
           FROM  (SELECT @b:= 0)r, T
         ORDER BY val) B
        ON B.n = A.n-1    /* step 3 */
     ORDER BY seq,val) s  /* step 5 */
    

    A word or two of explanation:

    1. We need to turn your val table into
      a val,n table (where n is the row
      number)

    2. Then we need two of those, with
      independent row numbers @a and @b

    3. Then we need to join them together
      with a row = row-1 offset to compare
      adjacent val columns.

    4. Then we need to apply a sequence
      number to each val column, that
      resets. That is, we need to give
      sequence numbers 1,2,3 to the 1s, then the 2s,
      etc.

    5. Then we need to sort by seq, then
      val.

    6. Then we need to conceal the seqs.

    Lo and behold….

    Hack hack!

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

Sidebar

Related Questions

I have a number of tables that use the trigger/sequence column to simulate auto_increment
I have problem in some JavaScript that I am writing where the Switch statement
I have problem when I try insert some data to Informix TEXT column via
I have a problem at hand which I'd think is fairly common amongst groups
I have a problem in Excel 2007. I need to compare one value -
i have a problem with my script i have a method that return a
I have a problem with this code, maybe you can give me a hand.
I have problem with return statment >.< I want to store all magazine names
I have problem with starting processes in impersonated context in ASP.NET 2.0. I am
I have problem compilin this code..can anyone tell whats wrong with the syntax CREATE

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.