如何选择具有名称的不同记录 [英] how to select distinct records with names
本文介绍了如何选择具有名称的不同记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一张这样的桌子
名字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
这篇关于如何选择具有名称的不同记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文