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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:55:21+00:00 2026-06-11T17:55:21+00:00

Here is the DB available. create table tab1 ( masterid integer(2) primary key, name

  • 0

Here is the DB available.

create table tab1 (
  masterid integer(2) primary key,
  name varchar(22),
  l2 integer(2)
);

insert into tab1 values(1,'g-1',50);
insert into tab1 values(2,'g-1',50);
insert into tab1 values(3,'g-1',50);
insert into tab1 values(50,'grup1',-1);
insert into tab1 values(5,'g-1',50);
insert into tab1 values(60,'grup2',-1);
insert into tab1 values(7,'g-2',60);
insert into tab1 values(8,'g-2',60);

Also available on: http://sqlfiddle.com/#!2/b7fe9/1

Result should be groupwise. For example, Row 50,grup1,-1 is having 50 which will work as primary key and whichever row has that masterID( herein case 50) in l2 column , that should be retrieved.So Basically so I want following output.

MasterId    Name    l2  
   50       grup1   -1
   1        g-11    50
   2        g-12    50
   3        g-13    50
   5        g-14    50
   60       grup2   -1
   7        g-21    60
   8        g-22    60

Note that All the group will have -1 value in L2 column.
I have tried many things but I am confused this output can be achieved or not.I will appreciate your help. Thank you.

  • 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-11T17:55:22+00:00Added an answer on June 11, 2026 at 5:55 pm

    Try this query:

    SELECT *
    FROM tab1
    -- Order records by "master id groups", first
    ORDER BY CASE WHEN l2 = -1 THEN Masterid ELSE l2 END,
    -- Within a "master id group", get the master first and then its children
             CASE WHEN l2 = -1 THEN 0        ELSE 1 END
    

    Optionally, you can amend more expressions to the ORDER BY clause, to order “siblings”

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

Sidebar

Related Questions

I have a table similar to this: CREATE TABLE example ( id integer primary
This is my table structure: CREATE table Credit(id integer, organisationid int, availableCredit int) INSERT
I am using the jquery plugin boilerplate ( available here ) to create an
I must have missed something here. I am trying to create a table but
I want to create a simple table and make it available for user to
Hi I have a code example available here: http://jsbin.com/oxoweh My problem is that I
I'm taking a look at some random Icecast playlists (available here: http://dir.xiph.org/index.php ) and
I'm trying to import the US Census cartographic boundary files (available here: http://www.census.gov/geo/www/cob/bdy_files.html )
I'm using the JQuery DropdownChecklist as available from here: http://code.google.com/p/dropdown-check-list/ Basically I'm using some
Here is my code : What i want is just to make scrollbar available

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.