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.
Try this query:
Optionally, you can amend more expressions to the
ORDER BYclause, to order “siblings”