Click here to Skip to main content
15,909,030 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a table like this

name Col1 Col2

srinivas 2000 NULL
srinivas NULL 3000
mahesh NULL 9000
mahesh 4000 NULL


i want output like this


name COl1 Col2
srinivas 2000 3000
mahesh 4000 9000

how to write the query please help
Posted

There is a simple way to do it for the data you show:
SQL
SELECT Name, MAX(Col1), MAX(Col2)
FROM MyTable
GROUP BY Name
But it may not work quite as you want if you have more than one non-null value.
 
Share this answer
 
Comments
Manas Bhardwaj 21-Apr-14 14:00pm    
This would work on the data set he showed.

However, I think he actually wants SUM. Please see my answer.
Try
select A.name,A.col1,(select col2 from table B where A.name = B.name and col2 !=null) from table A where col1 != null
 
Share this answer
 
Comments
Manas Bhardwaj 21-Apr-14 14:03pm    
I am sure you wanted to use IS NOT NULL instead of != null :)
Like this:

SQL
SELECT
    Name,
    SUM((ISNULL(Col1, 0))) AS Col1,
    SUM((ISNULL(Col2, 0))) AS Col2
FROM
    TEMP
GROUP BY Name
 
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