【问题标题】:Finding duplicate value and taking difference with other values with Excel/SQL使用 Excel/SQL 查找重复值并与其他值不同
【发布时间】:2017-07-23 13:50:28
【问题描述】:

我遇到了一个很奇怪的问题。我有很多包含日期列、ID 列等的数据。我想在两列中取 2 个日期的时间差(其中日期报告为 2017 年 7 月 16 日 18:42)。乍一看,简单的区别看起来很容易,但这里的问题是 ID 列。在 ID 列中,有很多 ID 也是重复的。所以以下是条件:

  1. 获取第一行 ID 的日期和时间的差异。喜欢A2-B2
  2. 当相同的ID再次出现时,取Date的差异,然后差异就像B2-A3。但 ID 应该来自同一组。

以下是行:

ID  Date 1  Date 2
5AB80D3A    7/10/2017 14:16 7/14/2017 11:38
5AB80D3A    7/14/2017 11:38 7/14/2017 12:48
5AB80D3A    7/14/2017 13:00 7/14/2017 19:09
5AB80D3A    7/14/2017 19:09 7/14/2017 21:09
5AB80D      7/14/2017 19:09 7/14/2017 21:09
5AB80D      7/14/2017 19:09 7/14/2017 21:09
5AB80A      7/14/2017 19:09 7/14/2017 21:09

如果这可以在 excel/SQL 中完成,那将会很有帮助。提前感谢您的指导。

【问题讨论】:

  • 如果您不使用 MySQL,请删除该标签。否则该问题具有误导性。
  • 我正在从 SQL 查询中获取数据。所以如果我可以在查询中构建这个语法,那么它也可以工作。
  • 你用的是什么sql server?
  • SQL 服务器 2012
  • 所以你想要 B 列中的最大值减去 A 列中 ID 相同的最小值?

标签: sql sql-server excel sql-server-2012


【解决方案1】:

看看下面的查询,它可能会指导你做你想做的事。

 ;with
Testdata as
(select Id,Date1 ,Date2 
,ROW_NUMBER() over(partition by Id order by Date1) rowNumber
from Q1
)
select currentData.ID,currentData.Date1,currentData.Date2
,case when currentData.rowNumber=1 then datediff(MINUTE,currentData.Date1,currentData.Date2) else datediff(MINUTE,currentData.Date1,prevData.Date2) End Diffs  
from testData currentData 
left join Testdata prevData on currentData.ID=prevData.ID and currentData.rowNumber=prevData.rowNumber+1

编辑

查看您的 cmets 后,我收到以下查询,希望对您有所帮助

 ;with
Testdata as
(select Id,Date1 ,Date2 
 ,ROW_NUMBER() over(partition by Id order by Date1) rowNumber
 from Q1
)
select currentData.ID,currentData.Date1,currentData.Date2
,case when currentData.rowNumber=1 then datediff(MINUTE,currentData.Date1,currentData.Date2) else datediff(MINUTE,currentData.Date2,nextData.Date1) End Diffs  
from testData currentData 
left join Testdata nextData on currentData.ID=nextData.ID and currentData.rowNumber=nextData.rowNumber-1

Edit2(使用 row_number & order by Date1 Descending 以获取每个 ID 的最后一行)

 ;with
Testdata as
(select Id,Date1 ,Date2 
 ,ROW_NUMBER() over(partition by Id order by Date1) rowNumber
 ,ROW_NUMBER() over(partition by Id order by Date2) rowNumberDesc
 from Q1
)
 select currentData.ID,currentData.Date1,currentData.Date2
 ,case when currentData.rowNumber=1 or currentData.rowNumberDesc=1  then datediff(MINUTE,currentData.Date1,currentData.Date2) else datediff(MINUTE,currentData.Date2,nextData.Date1) End Diffs  
 from testData currentData 
 left join Testdata nextData on currentData.ID=nextData.ID and currentData.rowNumber=nextData.rowNumber-1

【讨论】:

  • 我测试了它,它正在工作,你必须发布一些示例数据以及你想要得到的结果。
  • ID 日期 1 日期 2 差异 5AB80D3A 7/10/2017 14:16 7/14/2017 11:38 21:22 5AB80D3A 7/14/2017 11:38 7/14/2017 12: 48 0:00 5AB80D3A 7/14/2017 13:00 7/14/2017 19:09 0:12 5AB80D3A 7/14/2017 19:09 7/14/2017 21:09 0:00 5AB80D 7/15/2017 19:09 2017 年 7 月 15 日 23:09 22:00 5AB80D 2017 年 7 月 16 日 19:09 2017 年 7 月 16 日 21:09 20:00 5AB80A 2017 年 7 月 17 日 19:09 2017 年 7 月 17 日 21: 09 22:00
  • 最后一列是差异列。如果第一行给出日期 1 和 2 以及第二行以后的差异,则给出当前行的 date1 和前一行的 Date 2 的差异。
  • 我以为你想要第一条记录中 date1 和 date2 之间的差异,只有 ,然后在第二行中获取上一个中的 date2 和第二行中的 date1 之间的差异,依此类推.
  • 我没看懂你评论中 diff 列的数据。
猜你喜欢
  • 1970-01-01
  • 2019-04-08
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多