【问题标题】:SQL. I need connect two row based on datesSQL。我需要根据日期连接两行
【发布时间】: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 值在日期列中的作用是什么?您是否将日期存储为文本?

标签: mysql sql procedure


【解决方案1】:

如果我遇到您的情况,而不是尝试更新 testcheck 表,我会使用查询以我想要的格式提取数据,然后将其插入具有相同结构的新表中:

CREATE TABLE newtestcheck (id int, Date1 datetime, Date2 datetime, Date3 datetime,
                           Date4 datetime);

INSERT INTO newtestcheck
SELECT id,
       Date1,
       MAX(Date2),
       MAX(Date3),
       MAX(Date4)
FROM testcheck
GROUP BY id, Date1;

在此之后,您可以删除原始表,然后根据需要将 newtestcheck 重命名为 testcheck

注意:您在其中一个日期列中有一个看起来非常可疑的 N/A 数据点。如果是字面意思,那么这意味着您将日期存储为文本,然后我的查询将不得不被修改。

【讨论】:

  • 如果有两个 Date2 怎么办。 Date3 和 Date4 不同?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-24
  • 1970-01-01
  • 2022-07-25
  • 2021-02-23
  • 1970-01-01
  • 2020-01-07
  • 2018-09-23
相关资源
最近更新 更多