合并两行,其中 1 列具有值,另一列为空 [英] Combining two rows where 1 column has a value and another is null

查看:45
本文介绍了合并两行,其中 1 列具有值,另一列为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试合并两行(重复)

I am trying to combine two rows (duplicates) where either

1) 第一行为空,第二行为值(或反之亦然)2) 两行都为空.

1) Null in 1st Row and Value in 2nd Row (Or Vice-a-Versa) 2) Null in both the rows.

我需要将这两行合并以生成 1 行,其中该列有值(如果任一行中存在任何值)或空值.

I need to combine these two rows to produce 1 row where there are either values for that column (if any present in either of the rows) or a null.

例如.

需要转换为

我已经为相同的数据设置了SQL Fiddle here.如果您需要更多信息,请告诉我.

I have set up SQL Fiddle here for the same data. Please let me know if you need any more information.

到目前为止,我已经尝试使用 Full Outer Join(它不适用于这么多具有​​空值和内容的列)和 Pivot(我不确定它是否适合这个问题).

So far I have tried using Full Outer Join (which does not work with so many columns having nulls and stuff) and Pivot (which I am not sure is the right fit for this problem or not).

SELECT A.EmployeeID, A.PostEditDate, 
A.In1
, A.Out2, A.In3, A.Out4, A.Out5, A.Out6, A.Out7, A.Out8, A.Out9, A.Out10  FROM #TempTableRV A
full outer JOIN #TempTableRV B
On A.EmployeeID = B.EmployeeID AND A.PostEditDate = B.PostEditDate
where 
A.PostEditDate = '2007-06-19 00:00:00.000' 
AND 
A.EmployeeID = 25690 and A.In1 IS NULL AND B.In1 IS NULL 

推荐答案


SELECT

EmployeeID,
MAX(PostEditDate) AS PostEditDate,
MAX(In1) AS In1,
MAX(Out2) AS Out2,
MAX(In3) AS In3,
MAX(Out4) AS Out4,
MAX(Out5) AS Out5,
MAX(Out6) AS Out6,
MAX(Out7) AS Out7,
MAX(Out8) AS Out8,
MAX(Out9) AS Out9,
MAX(Out10) AS Out10

FROM

#TempTableRV

GROUP BY
EmployeeID

这篇关于合并两行,其中 1 列具有值,另一列为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆