Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a table like this..

Column1   Column2   Column3
-------   -------   --------
 1         NULL      NULL
 NULL       2        NULL
 NULL      NULL       3



I want to get an out put like this..

Column1   Column2   Column3
----------------------------
 1         2           3


Please help me...I am using MSSQL 2008..
Posted
Updated 16-Aug-12 20:52pm
v4
Comments
__TR__ 17-Aug-12 2:13am    
Will there be more than 3 rows in the table?
How do you want your output to be if there are more than 3 rows.
Ex:
Column1 Column2 Column3
------- ------- --------
1 NULL NULL
NULL 2 NULL
NULL NULL 3
4 NULL NULL
OriginalGriff 17-Aug-12 2:18am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalisation if you want to be taken seriously.
kanamala subin 17-Aug-12 2:25am    
If more than 3 columns..........
Column1 Column2 Column3
------- ------- --------
1 NULL NULL
NULL 2 NULL
NULL NULL 3
4 NULL NULL

Out put is

Column1 Column2 Column3
------- -------------------
1 2 3
4

Thats all..

Out put is like
StianSandberg 17-Aug-12 3:00am    
I have no idea why you want to do this, but I don't think you'll find a smart/easy way of doing this in sql. A Database is ment for data, not logic. I think you should treat data as data and when you need to manipulate it like this, take it out to your program and to all logic there..
Sebastian T Xavier 17-Aug-12 2:53am    
Please post your query

This is simply impossible because you are combining fields of rows that should be independent of each other. You cannot assume any sort order and need information about the contents of each row. Also, you now have some relation between otherwise unrelated data. For example, you assume:
1 NULL NULL
NULL 2 NULL
NULL NULL 3
4 NULL NULL

But it could also easily be:
NULL NULL 3
4 NULL NULL
1 NULL NULL
NULL 2 NULL

Would this yield a desired result?

It looks like you have 2 options:
First is a simple solution to get some result that might even look what you want by simply doing 3 select operations, each to get values for column1, column2 and column3. After that you combine those results to get the end result.

Second, it looks like you are combining columns in a row that shouldn't. It is like you created a separate column for each option but with a constraint that excludes all other options. Example columns: Male, Female, Unknown gender

Maybe a good time to read up on normalization:
http://msdn.microsoft.com/en-us/library/aa933055%28v=sql.80%29.aspx[^]
http://msdn.microsoft.com/en-us/library/aa291817%28v=vs.71%29.aspx[^]

Good luck!
 
Share this answer
 
Its Simple friend,

Use Max() It will work.

Eg.:- Select Column1=Max(Column1),Column2=Max(Column2),Column3=Max(Column3)
 
Share this answer
 

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

  Print Answers RSS


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