使用Excel/SQL查找重复值并与其他值求差
我有一个非常奇怪的问题。我有很多数据包含日期列、ID列等。我想在两列中取两个日期的时间差(其中日期报告为2017年7月16日18:42)。在第一个实例中,简单的区别看起来很容易,但这里的问题是ID列。在ID列中,有很多ID,其中ID也是重复的。因此,以下是条件:使用Excel/SQL查找重复值并与其他值求差,sql,sql-server,excel,sql-server-2012,Sql,Sql Server,Excel,Sql Server 2012,我有一个非常奇怪的问题。我有很多数据包含日期列、ID列等。我想在两列中取两个日期的时间差(其中日期报告为2017年7月16日18:42)。在第一个实例中,简单的区别看起来很容易,但这里的问题是ID列。在ID列中,有很多ID,其中ID也是重复的。因此,以下是条件: 取日期和时间的差异,其中第一行ID为A2-B2 当相同的ID再次出现时,取日期的差值,则差值将类似于B2-A3。但ID应该来自同一组 以下是行: ID Date 1 Date 2 5AB80D3A 7/10/2017 14:1
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中完成,它将非常有用。提前感谢您的指导。看看下面的查询,它可能会引导您做您想做的事情
;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
编辑
在审阅了你的评论后,我得到了以下问题,希望能对你有所帮助
;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(使用行号和按日期排序1降序以获取每个ID的最后一行)
如果您没有使用MySQL,请删除标记。否则,这个问题就有误导性了。我正在从SQL查询中获取数据。因此,如果我可以在查询中构建此syntex,那么它也可以工作。您使用的sql server是什么?sql server 2012是否希望B列中的最大值减去A列中ID相同的最小值?我测试了它,它可以工作,您必须发布一些示例数据以及您想要获得的结果。ID Date 1 Date 2 Diff 5AB80D3A 7/10/2017 14:16 7/14 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 5AB80D2017年7月15日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的差值,第二行之后给出当前行的日期1和前一行的日期2的差值。我以为您只想在第一条记录中得到日期1和日期2之间的差值,然后在第二行中得到前一行的日期2和第二行的日期1之间的差值,等等。我不理解你评论中diff列中的数据。
;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