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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:59:42+00:00 2026-05-26T07:59:42+00:00

I have following data GROUP_ID INDEX NULL 1 1 2 NULL 3 1 4

  • 0

I have following data

 GROUP_ID   INDEX
   NULL       1
     1        2
   NULL       3
     1        4
   NULL       5
     2        6
     2        7

What I want is to get a field that contains:

  • if group_id IS NOT NULL, a smallest index in that group
  • if group_id IS NULL, then index from that row

The result dataset for the above example would be:

 GROUP_ID   INDEX  GROUP_INDEX_MIN
   NULL       1          1
     1        2          2
   NULL       3          3
     1        4          2
   NULL       5          5
     2        6          6
     2        7          6

In Oracle I would resolve this by using (CASE WHEN group_id IS NOT NULL THEN MIN(index) OVER (PARTITION BY group_id) ELSE group_id END), but since MySQL does not support that, I really don’t know how to proceed 🙂 I can solve this by using a subquery that retrieves minimal values for each group and then left joining to it, but I think that there must be a more elegant solution.

If you need any more information, please ask.
Thanks.

  • 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-26T07:59:42+00:00Added an answer on May 26, 2026 at 7:59 am

    Just create an inline view representing the min Group_index_min (minT) and then join to it and do a coalesce between group_ID and the min

    SELECT t.group_id, 
           t.index, 
           Coalesce(t.group_id, minTgroup_index_min) group_index_min 
    FROM   yourtable t 
           LEFT JOIN (SELECT group_id, 
                             MIN(index) group_index_min 
                      FROM   yourtable
                      GROUP BY 
                             group_id) minT 
             ON t.group_id = minT.group_id 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following data in my table. alt text http://img26.imageshack.us/img26/3746/productfield.png I want to extract
I have the following data set that I am trying to plot with ggplot2,
I have the following example data in javascript: var variations = [ {group: 1,
I have the following query that receives a list of IDs and I want
This is for MySQL and PHP I have a table that contains the following
I have a database with the following stats Tables Data Index Total 11 579,6
I have the following table with data: t1 (results): card_id group_id project_id user_id The
Hello, I have the following sql table + data: CREATE TABLE IF NOT EXISTS
I have following data to save in database using php my data is :
Hi I have following data in the table: ID-----startDate----endDate 5549 2008-05-01 4712-12-31 5567 2008-04-17

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.