SQL - 从2个表中获取信息时匹配数据的问题`duplicant'

时间:2014-07-31 11:11:02

标签: sql

我想查询一下我可以监控以下内容:

我们部门的某人向某人发送任务以及我们的部门收到回复消息的时间。

每个任务都可以分成多个消息,其中大部分都有2个(发送和接收)其他更多像4+(2x发送和2x接收)。 我在一个不同的临时表中发送和接收消息; TaskTo,TaskBack。所有消息都有时间戳。他们也有描述,在这种情况下无所谓。

当我在1个任务中有4个或更多消息时,问题就开始了:

Tables:
Table = #TaskTo 
╔════════╦════════╦══════╗
║ ID     ║ Time   ║ Desc ║
╠════════╬════════╬══════╣
║      1 ║ 14:25  ║ x    ║
║      2 ║ 16:00  ║ x    ║
║      2 ║ 11:00  ║ x    ║
║      3 ║ 15:02  ║ x    ║
╚════════╩════════╩══════╝

Table = #TaskBack
╔════════╦════════╦══════╗
║ ID     ║ Time   ║ Desc ║
╠════════╬════════╬══════╣
║      1 ║ 14:30  ║ x    ║
║      2 ║ 19:00  ║ x    ║
║      2 ║ 12:01  ║ x    ║
║      3 ║ 17:20  ║ x    ║
╚════════╩════════╩══════╝

=================================

我有以下SQL查询:

SELECT DISTINCT                         
to.ID, 
to.time as time1,
Bk.time as time2, 
DATEDIFF (HOUR, to.EndTask,Bk.EndTask) as diff

FROM #TaskTo as to LEFT JOIN #TaskBack as Bk on to.TicketNumber = Bk.Ticketnumber
ORDER BY to.ID

=================================

这就是我得到的结果:

╔════════╦════════╦══════╦══════╗
║ ID     ║ Time1  ║ time2║ diff ║
╠════════╬════════╬══════╬══════╣
║      1 ║ 14:25  ║ 14:30║0     ║
║      2 ║ 16:00  ║ 19:00║3     ║
║      2 ║ 16:00  ║ 12:01║20    ║
║      2 ║ 11:00  ║ 19:00║8     ║
║      2 ║ 11:00  ║ 12:01║1     ║
║      3 ║ 15:02  ║ 17:20║2     ║
╚════════╩════════╩══════╩══════╝

因此问题是数据为Duplicant

║      2 ║ 16:00  ║ 19:00║3     ║
║      2 ║ 16:00  ║ 12:01║20    ║
║      2 ║ 11:00  ║ 19:00║8     ║
║      2 ║ 11:00  ║ 12:01║1     ║

梦想的场景是:

║      2 ║ 16:00  ║ 19:00║3     ║
║      2 ║ 11:00  ║ 12:01║1     ║

任何人都可以解释我的错误吗? :)

感谢您的时间。

亲切的问候, 微米。

2 个答案:

答案 0 :(得分:1)

我在这里使用保留关键字的T-SQL语法。如果您不使用SQL Server,请修改给其他人。

SELECT
[to].ID, 
[to].[time] as time1,
[Bk].[time] as time2, 
DATEDIFF (HOUR, [to].EndTask,[Bk].EndTask) as diff
FROM 
(
    SELECT Id, [Time], [Desc],
    LEAD([Time]) OVER (PARTITION BY Id ORDER BY [Time]) AS NextTime
    FROM #TaskTo
) AS [To]
LEFT JOIN
(
    SELECT Id, [Time], [Desc],
    FROM #TaskBack
) AS Bk on [to].Id = Bk.Id AND [to].[Time] < [bk].[Time] AND ([to].NextTime > [bk].Time OR [to].NextTime IS NULL)
ORDER BY [to].ID

答案 1 :(得分:0)

我可以立即看到的问题:

  • 当然,您向我们展示的表格中没有唯一性 你最终会得到重复的。
  • 您的查询与您的表格图不符(您不必提及 TicketNumber在图表中的任何位置。)
  • 您使用sql reserved words作为表别名和字段名。

我创建了一个SQL fiddle,也许你可以扩展它以便更贴近你的情况,包括你问题中修改过的sql小提琴......

以下是修订后的SQL fiddle

以下是代码示例:

SELECT  tto.ID, 
        tto.[time] as time1,
        tbk.[time] as time2, 
        DATEDIFF (HOUR, tto.[time],tbk.[time]) as diff
FROM       (SELECT  ID,
                    [Time],
                    Descr, ROW_NUMBER() OVER (ORDER BY 
                    ID,
                    [Time]) AS rowId
            FROM TaskTo) tto
LEFT JOIN (SELECT   ID,
                    [Time],
                    Descr, ROW_NUMBER() OVER (ORDER BY 
                    ID,
                    [Time]) AS rowId
            FROM TaskBack
            ) AS tbk
    ON tto.rowid = tbk.rowid

结果:

ID  TIME1               TIME2               DIFF
1   14:25:00.0000000    14:30:00.0000000    0
2   11:00:00.0000000    12:01:00.0000000    1
2   16:00:00.0000000    19:00:00.0000000    3
3   15:02:00.0000000    17:20:00.0000000    2

请注意,这些结果基于非常小的数据样本。