Click here to Skip to main content
15,889,909 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I have table like this
ID Name
1 Naveen
2 Naveen
3 Naveen
4 Praveen
5 Praveen


I need result mentioned below, If required u can create new columns as col1,col2 and Col3 or you u use select statement only by using case statement.

ID Name <>Col1<>Col2<>Col3
1 Naveen <>1<>null<>null
2 Naveen <>null<>2<>null
3 Naveen <>null<>null<>3
4 Praveen <>1<>null<>null
5 Praveen <>null<>2<>null

Edit - content of solution posted by OP
ID Name      Col1     Col2	    Col3
1  Naveen    1	      null	         null
2  Naveen    null     2             null
3  Naveen    null     null          3
4  Praveen   1        null          null
5  Praveen   null     2             null



Yes exact output i required.... Please share query of that above ssolution
Posted
Updated 16-Jul-15 2:23am
v6
Comments
gregorio89 16-Jul-15 3:32am    
you can have only 3 rows for each name?
Tryingtobeageek 16-Jul-15 3:57am    
no for col1 is '1' col2 is '-' and col3 is '-' for ID 1
same for ID 2 col1 is '-' col2 is '2' and col3 is '-'
gregorio89 16-Jul-15 4:06am    
so the question was:
how many rows can you have for same name?
i see 3 rows for name Naveen, 2 for praveen.
you have always 3 rows for each name?
Tryingtobeageek 16-Jul-15 5:07am    
and consider col1 , col2,col3 as a separate columns in tables
Tryingtobeageek 16-Jul-15 5:09am    
under those columns need result

ID Name Col1 Col2 Col3
1 Naveen 1 - -
2 Naveen - 2 -
3 Naveen - - 3

1 solution

With your very small sample set and lack of rules, this will get you what you want.

SQL
select [id], name,
        case when [id] % 3 = 1 then 1 end as col1,
        case when [id] % 3 = 2 then 2 end as col2,
        case when [id] % 3 = 0 then 3 end as col3
from names


If you need more Coln columns, or there are more than three of each name, then it won't work.

If you;re trying to say that "ID is the nth instance of Name" then you might need to parse the info into a temp table and look at pivoting the results to get the coln.
 
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