Click here to Skip to main content
Sign Up to vote bad
good
See more: SQL2008
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 16 Aug '12 - 20:03
Edited 16 Aug '12 - 20:52

Comments
__TR__ - 17 Aug '12 - 2:13
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:18
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:25
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
AlluvialDeposit - 17 Aug '12 - 3:00
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:53
Please post your query

2 solutions

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!
  Permalink  
Its Simple friend,
 
Use Max() It will work.
 
Eg.:- Select Column1=Max(Column1),Column2=Max(Column2),Column3=Max(Column3)
  Permalink  

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 239
1 OriginalGriff 188
2 Mahesh Bailwal 149
3 Maciej Los 140
4 Tadit Dash 128
0 Sergey Alexandrovich Kryukov 10,214
1 OriginalGriff 7,819
2 CPallini 4,181
3 Rohan Leuva 3,522
4 Maciej Los 3,089


Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 17 Aug 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid