Click here to Skip to main content
15,905,073 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
Need help to create a table structure:
I have a table structure like this :

i_d         I_d_value            Name      Message
1           11                   test1     test11 message
1           12                   test1     test12 message
1           13                   test1     test13 message
2           21                   test2     test21 message
2           22                   test2     test22 message
2           23                   test2     test23 message
2           24                   test2     test24 message
3           31                   test3     test31 message


and i want to create procedure to manage the structure like:

i_d             I_d_value            Name      Message
1               11                   test1     test11 message
                12                   test1     test12 message
                13                   test1     test13 message
2               21                   test2     test21 message
                22                   test2     test22 message
                23                   test2     test23 message
                24                   test2     test24 message
3               31                   test3     test31 message


using sql for this.
Thanks in advance.

What I have tried:

tried to handle in gridview but couldn't succeed. But now want to do with sql only.
Posted
Updated 11-Sep-17 1:49am
v3
Comments
Afzaal Ahmad Zeeshan 11-Sep-17 7:25am    
To my eyes, that looks a lot inconsistent. Why is the i_d field being removed? Any logical reason?

Also, in the first data set there seems to be 5 values (if I am not applying a delimiter to the data), you need to properly explain which parts go where.
RedDk 11-Sep-17 13:33pm    
This is the first case I've ever seen here in TSQL question which I didn't close the door on thinking outside the box but there's a first time for everthing perhaps. Understand that having a value in a column full of mixed types is going to completely douche your ability to SORT on that column.

But ... do what OG says ... if not, perhaps at some point all you want to do is SORT then DELETE via TOP, right?

1 solution

You can do it ... but it's not a good use of SQL, that kind of thing is much better done in a presentation language rather than in SQL.
In SQL its like this:
SQL
SELECT CASE WHEN I_D = LAG(I_D) OVER (ORDER BY I_D, I_D_Value) THEN NULL ELSE I_D END, 
       I_D_Value, [Name], [Message] FROM MyTable
 
Share this answer
 
v2

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