【发布时间】:2017-03-29 07:48:50
【问题描述】:
一开始我真的很抱歉我的英语不好。
我需要比较具有相同 id 的行,如果它们正确连接它们:
id Date1 Date2 Date3 Date4
1210 2013-01-09 NULL 2018-04-10 2023-04-11
1210 2013-09-01 2018-10-04 2023-11-04 NULL
83 2009-11-17 NULL 2014-11-30 2016-11-30
83 2009-11-17 NULL NULL 2016-11-30
198 2008-04-22 NULL 2013-04-30 2015-04-30
198 2008-04-22 2013-04-30 2014-04-30 2015-04-30
198 2008-04-22 NULL NULL NULL
2070 1997-06-18 NULL 2002-09-30 N/A
2070 1997-06-18 2001-09-30 2002-09-30 NULL
2070 1997-06-18 NULL NULL 2002-09-30
应该删除我已经从中获取数据的行。
效果应该是:
id Date1 Date2 Date3 Date4
1210 2013-01-09 NULL 2018-04-10 2023-04-11
1210 2013-09-01 2018-10-04 2023-11-04 NULL
83 2009-11-17 NULL 2014-11-30 2016-11-30
198 2008-04-22 NULL 2013-04-30 2015-04-30
198 2008-04-22 2013-04-30 2014-04-30 2015-04-30
2070 1997-06-18 2001-09-30 2002-09-30 2002-09-30
1210 - 未更改,因为部分日期不同。
83 - 进行比较,然后删除数据较少的行。
198 - 匹配的行数据分配给第一个匹配的行并删除该行。第二行不变,因为部分日期不同。
2070 - 所有行合并为一个。附加的行被删除。
我尝试过制作代码:
update tb
set tb.Date1 = case
when tj.Date1 is not null and (tb.Date1 is null or tb.Date1 = 'n/a') then tj.Date1 end,
tb.Date2 = case
when tj.Date2 is not null and (tb.Date2 is null or tb.Date2 = 'n/a') then tj.Date2 end,
tb.Date3 = case
when tj.Date3 is not null and (tb.Date3 is null or tb.Date3 = 'n/a') then tj.Date3 end,
tb.Date4 = case
when tj.Date4 is not null and (tb.Date4 is null or tb.Date4 = 'n/a') then tj.Date4 end
from
testcheck as tb inner join testcheck as tj on tb.Product_ID= tj.Product_ID
where (tb.Date1 = tj.Date1 or tb.Date1 is null or tj.Date1 is null or tb.Date1 = 'n/a' or tj.Date1 = 'n/a')
and (tb.Date2 = tj.Date2 or tb.Date2 is null or tj.Date2 is null or tb.Date2 = 'n/a' or tj.Date2 = 'n/a')
and (tb.Date3 = tj.Date3 or tb.Date3 is null or tj.Date3 is null or tb.Date3 = 'n/a' or tj.Date3 = 'n/a')
and (tb.Date4 = tj.Date4 or tb.Date4 is null or tj.Date4 is null or tb.Date4 = 'n/a' or tj.Date4 = 'n/a')
【问题讨论】:
-
N/A值在日期列中的作用是什么?您是否将日期存储为文本?