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

The Archive Base Latest Questions

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

I have a select in which I do a group by. One of the

  • 0

I have a select in which I do a “group by”. One of the columns is an enum, and I want to select the biggest value for each group (i.e the one with the largest index in the enum). I can do

select MAX(enum_column+0) as enum_index 

to get the the largest index in the group, but how can I turn the enum index back to the enum item?


Example:
Say I have a table “soldiers” with three columns:

"id" - soldier's ID
"name" is the soldier's first name (a varchar field).
"rank" is the soldier's rank, and it is an enum: 
{'private','sergent'...,'lieutenant' etc.} 

Now suppose I want to find for each first name, the the highest rank a person with that name has.
I can do:

select MAX(rank+0) as enum_index,name from soldiers group by name

But that will give me the index of the max element in the enum field, and not the name of the element. I.e it will give me:

1 | john
2 | bob

where I want

'private' | john
'sergent' | bob 

How can I achieve the desired result?

  • 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-12T13:30:44+00:00Added an answer on May 12, 2026 at 1:30 pm

    Run the following

    CREATE TABLE ranks
    SELECT DISTINCT rank+0 as id, CONCAT('',rank) as rank
    FROM soldiers
    

    Then join the data with the soldiers table. Notice it will give you only the ranks actually in use.

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

Sidebar

Related Questions

I have several Select which depend on each other if I select a value
i have some queries which group datasets and count them, e.g. SELECT COUNT(*) FROM
I have this query which groups the results by ORDER#. SELECT ORDER#, MAX(SHIPDATE -
i have a table with columns ID,SUBJECT,BRANCH i have select the rows which satisfies
I have 6 columns in a table. I have a select query which selects
I have a table which has no primary key and I can't add one
I have code which will only let me select 22 columns then comes up
Ok so I have a table in which ONE of the columns have a
I have an EnumerableRowCollection in C# .Net, with two columns. I want to Group
Given i have articles which are tagged, one article can have n tags. Currently

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.