Click here to Skip to main content
15,894,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
col1 col2 col3
100 null null
null 200 null
null null 300



output
col1
100
200
300

col col2 col3
100 200 300
Posted
Comments
Jörgen Andersson 13-Feb-15 6:57am    
Is this a table you want to fix or the result of a query?

That is a totally wrong method or output that you're trying to perform and get. A table, is more like an object in real-world, sometime their data is null when they don't have anything specific to it. This output of yours, seems like arm of someone, leg of someone and heart of someone else. That is totally a bad call.

Anyhow, to remove the records where the values are null, you can use this following command,

SQL
DELETE FROM table_name WHERE column_name IS NULL


The above would remove those records, where the column_name is a null value. But, do not try to combine different records and make up one record, only because they were having their other fields left as null. That is not how database can be compact.
 
Share this answer
 
v2
I'm not sure you are trying to do the right thing: what happens if there are two rows with values in col1? I suspect you need to rethink your data structure a little.

But:
The first output version:
SQL
SELECT ISNULL(col1, 0)+ISNULL(col2, 0)+ISNULL(col3, 0) FROM MyTable

The second:
SQL
SELECT SUM(col1), SUM(col2), SUM(col3) FROM MyTable
 
Share this answer
 
Don't know what exactly you want but an alternative approach could be like

SQL
CREATE GLOBAL TEMPORARY TABLE TMP_TBLE
    (COL1 INT,
     COL2 INT,
     COL3 INT)
  ON COMMIT DELETE ROWS;

INSERT INTO TMP_TBLE(COL1, COL2, COL3)
SELECT 100, NULL, NULL
UNION ALL
SELECT NULL, 200, NULL
UNION ALL
SELECT NULL, NULL, 300

SELECT COALESCE(COL1, COL2, COL3)
FROm TMP_TBLE

SELECT  SUM(COL1) COL1, SUM(COL2) COL2, SUM(COL3) COL3
FROm TMP_TBLE
 
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