如何选择具有名称的不同记录

问题描述:

我有一张这样的桌子



名字Col1 Col2



srinivas 2000 NULL

srinivas NULL 3000

mahesh NULL 9000

mahesh 4000 NULL





i想要这样的输出





名称COl1 Col2

srinivas 2000 3000

mahesh 4000 9000



怎么写查询请帮忙

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

有一个为您显示的数据执行此操作的简单方法:

There is a simple way to do it for the data you show:
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.


尝试

selec t A.name,A.col1,(从表B中选择col2,其中A.name = B.name和col2!= null)来自表A,其中col1!= null


像这样:



Like this:

SELECT
    Name,
    SUM((ISNULL(Col1, 0))) AS Col1,
    SUM((ISNULL(Col2, 0))) AS Col2
FROM
    TEMP
GROUP BY Name