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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:06:17+00:00 2026-06-04T13:06:17+00:00

I have a table with the following (simplified) structure: INT id, INT type, INT

  • 0

I have a table with the following (simplified) structure:

INT id,
INT type,
INT sort

What I need is a SELECT that sorts my data in a way, so that:

  • all rows of the same type are in sequency, sorted ascendingly by sort internally, and
  • all “blocks” of one type are sorted by their minimum sort.

Example:

If the table looks like this:

| id | type | sort |
|  1 |   1  |   3  |
|  2 |   3  |   5  |
|  3 |   3  |   1  |
|  4 |   2  |   4  |
|  5 |   1  |   2  |
|  6 |   2  |   6  |

The query should sort the result like this:

| id | type | sort |
|  3 |   3  |   1  |
|  2 |   3  |   5  | 
|  5 |   1  |   2  |
|  1 |   1  |   3  | 
|  4 |   2  |   4  |
|  6 |   2  |   6  | 

I hope this makes it clear enough.

Looks to me, as this should be a very common requirement, but I didn’t find any examples close enough to be able to transfer it to my use case on my own. I suppose I can’t avoid at least one subquery, but I didn’t figure it out on my own.

Any help is appreciated, thanks in advance.

By the way: I’m going to use this query with CakePHP 2.1, so if you know of a comfortable way to do it with Cake, please let me know.

  • 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-06-04T13:06:19+00:00Added an answer on June 4, 2026 at 1:06 pm

    This is simpler than it initially sounds. I believe the following should do the trick:

    SELECT a.id, a.type, a.sort
    FROM Some_Table as a
    JOIN (SELECT type, MIN(sort) as min
          FROM Some_Table
          GROUP BY type) as b
    ON b.type = a.type
    ORDER BY b.min, a.type, a.sort
    

    For best (fastest) results, you’re probably going to want an index on (type, sort).
    You want an additional sort by a.type (instead of (b.min, a.sort)), in case there are two groups with the same sort value (would result in mixed rows). If there are no duplicate values, you can remove it.

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

Sidebar

Related Questions

I have a table with the following simplified data: PK FK Type ---------------- 1
I have no control over database schema and have the following (simplified) table structure:
I have two tables with the following (simplified) structures: table "Factors" which holds data
I have a MySQL table with the following data (simplified): INSERT INTO `stores` (`storeId`,
I have the following problem (simplified): I have a table that contains animals, e.g:
I have the following (simplified) result from SELECT * FROM table ORDER BY tick,refid
I have a table that looks like the following (simplified): <table id=product_table> <tbody> <tr>
I have the following table structure (simplified version) +----------------+ +-----------------+ +------+ | fee_definition |
I have following table Managers (simplified): ID, int Name, nvarchar(100) In a stored procedure
I have the following simplified table structure in a SQL 2000 Databse: ID AppName

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.