Click here to Skip to main content
16,020,811 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to query this table

SQL
txnno       Name		type	isHeadr	headr
    1000	BEFORE	        JO	Y	
    1001	WHILE		JO	Y	
    1002	data aaa	JO	N	1000
    1003	bbbbdfds	JO	N	1000
    1004	ccccccc	        JO	N	1000
    1012	aaa111		JO	N	1001
    1015	bbb222		JO	N	1001
    1016	ccc333		JO	N	1001


to get this result in MY SQL SERVER

SQL
txnno	Name		type	isHeadr	headr
    1000	BEFORE	        JO	Y		
    1002	data aaa	JO	N	1000
    1003	bbbbdfds	JO	N	1000
    1004	ccccccc	        JO	N	1000
    1001	WHILE		JO	Y
    1012	aaa111		JO	N	1001
    1015	bbb222		JO	N	1001
    1016	ccc333		JO	N	1001


need to arrange the table to parent child format

What I have tried:

i tried to join the same table
Posted
Updated 15-Aug-16 2:20am
v4
Comments
Maciej Los 15-Aug-16 6:21am    
You have to use ORDER BY instruction.
W Balboos, GHB 15-Aug-16 6:37am    
You source (first) table is ordered by the first column [txnno].
You target table doesn't seem to be ordered by anything - could you please explain what it is that gives that particular order to your table?

For what you want, all I can envision is a brute-force methodology that would be tedious with the eight entries you have and ludicrous for a table of any real size.<br>

1 solution

I got the solution

SQL
SELECT *
FROM YourTable
ORDER BY ISNULL(headr,txnno) ASC, txnno
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900