我正在尝试使用以下数据加入2个表。当我使用下面的查询加入时,它会获得多个重复的行。我使用了不同的。
提前道歉,说明帖子不正确或不充分。请耐心等待,因为这是我第一次在这个论坛上发帖提问。
我正在使用以下查询。
select a.timestamp_for_start_message, b.timestamp_for_start_message
from start_message a, success_message b where a.id1 = b.id1 and a.jobid = b.jobid;
启动消息表
ID1 Timestamp_start_msg_recieved date jobid message
1234 5/14/2014 10:02:29 5/14/2014 abc start
1234 5/14/2014 10:02:29 5/14/2014 abc start
1234 5/14/2014 10:02:29 5/14/2014 abc start
1234 5/14/2014 10:02:29 5/14/2014 abc start
1234 5/14/2014 10:09:06 5/14/2014 abc start
1234 5/14/2014 10:09:06 5/14/2014 abc start
1234 5/14/2014 10:09:06 5/14/2014 abc start
1234 5/14/2014 10:09:06 5/14/2014 abc start
1234 5/14/2014 11:45:11 5/14/2014 abc start
1234 5/14/2014 11:45:11 5/14/2014 abc start
1234 5/14/2014 11:45:11 5/14/2014 abc start
1234 5/14/2014 11:45:11 5/14/2014 abc start
1234 5/14/2014 19:02:52 5/14/2014 abc start
5678 5/14/2014 22:02:52 5/14/2014 pqr start
5678 5/14/2014 23:10:40 5/14/2014 pqr start
成功消息表
ID1 Timestamp_success_msg_recieved date jobid message
1234 5/14/2014 10:02:31 5/14/2014 abc successful
1234 5/14/2014 10:02:31 5/14/2014 abc successful
1234 5/14/2014 10:02:31 5/14/2014 abc successful
1234 5/14/2014 10:02:31 5/14/2014 abc successful
1234 5/14/2014 10:09:10 5/14/2014 abc successful
1234 5/14/2014 10:09:10 5/14/2014 abc successful
1234 5/14/2014 10:09:10 5/14/2014 abc successful
1234 5/14/2014 10:09:10 5/14/2014 abc successful
1234 5/14/2014 16:48:22 5/14/2014 abc successful
1234 5/14/2014 19:03:00 5/14/2014 abc successful
5678 5/14/2014 22:03:00 5/14/2014 pqr successful
我试图在这两个表的时间戳之间得到最接近的匹配。 以下是另一种查看数据的方法。 要满足的条件:一个启动消息可以具有多个成功消息,其具有相同或不同的时间戳,用于相同的id和jobid。我需要选择最大开始消息到成功消息的最小消息,以使其匹配。
id Time_stamp_message_recieved message jobid
1234 5/14/2014 10:02:29 started abc
1234 5/14/2014 10:02:29 started abc
1234 5/14/2014 10:02:29 started abc
1234 5/14/2014 10:02:29 started abc
1234 5/14/2014 10:02:31 successful abc
1234 5/14/2014 10:02:31 successful abc
1234 5/14/2014 10:02:31 successful abc
1234 5/14/2014 10:02:31 successful abc
1234 5/14/2014 10:09:06 started abc
1234 5/14/2014 10:09:06 started abc
1234 5/14/2014 10:09:06 started abc
1234 5/14/2014 10:09:06 started abc
1234 5/14/2014 10:09:10 successful abc
1234 5/14/2014 10:09:10 successful abc
1234 5/14/2014 10:09:10 successful abc
1234 5/14/2014 10:09:10 successful abc
1234 5/14/2014 11:45:11 started abc
1234 5/14/2014 11:45:11 started abc
1234 5/14/2014 11:45:11 started abc
1234 5/14/2014 11:45:11 started abc
1234 5/14/2014 16:48:22 successful abc
1234 5/14/2014 19:02:52 started abc
1234 5/14/2014 19:03:00 successful abc
PS:我正在基于两个表中的ID和JOBID以及TIMESTAMP进行排序,然后根据ID和JOBID加入。
ID1 IMESTAMP_for_start_message TIMESTAMP_for_start_message Date Jobid start_message
1234 5/14/2014 10:02:29 5/14/2014 10:02:31 5/14/2014 abc
1234 5/14/2014 10:09:06 5/14/2014 10:09:10 5/14/2014 abc
1234 5/14/2014 11:45:11 5/14/2014 16:48:22 5/14/2014 abc
1234 5/14/2014 19:02:52 5/14/2014 19:03:00 5/14/2014 abc
5678 5/14/2014 22:02:52 5/14/2014 22:03:00 5/14/2014 pqr
5678 5/14/2014 23:10:40 null 5/14/2014 pqr
**********************以下编辑问题********************** *******
假设我说下面提到的数据,我希望得到最大的开始消息和相应的最小或成功消息。
启动消息表ID1 Timestamp_start_msg_recieved date jobid message
1234 5/14/2014 10:02:29 5/14/2014 abc start
1234 5/14/2014 10:02:31 5/14/2014 abc start
1234 5/14/2014 10:02:45 5/14/2014 abc start
1234 5/14/2014 10:02:50 5/14/2014 abc start
1234 5/14/2014 10:09:04 5/14/2014 abc start
1234 5/14/2014 10:09:06 5/14/2014 abc start
1234 5/14/2014 10:09:16 5/14/2014 abc start
1234 5/14/2014 10:09:26 5/14/2014 abc start
1234 5/14/2014 11:45:11 5/14/2014 abc start
1234 5/14/2014 16:48:20 5/14/2014 abc start
1234 5/14/2014 19:02:52 5/14/2014 abc start
5678 5/14/2014 22:02:52 5/14/2014 pqr start
5678 5/14/2014 23:10:40 5/14/2014 pqr start
成功消息表
ID1 Timestamp_success_msg_recieved date jobid message
1234 5/14/2014 10:02:52 5/14/2014 abc successful
1234 5/14/2014 10:09:32 5/14/2014 abc successful
1234 5/14/2014 11:45:15 5/14/2014 abc successful
1234 5/14/2014 11:45:18 5/14/2014 abc successful
1234 5/14/2014 11:45:19 5/14/2014 abc successful
1234 5/14/2014 11:45:25 5/14/2014 abc successful
1234 5/14/2014 16:48:22 5/14/2014 abc successful
1234 5/14/2014 19:03:00 5/14/2014 abc successful
5678 5/14/2014 22:03:00 5/14/2014 pqr successful
ID1 IMESTAMP_for_start_message TIMESTAMP_for_start_message Date Jobid msg msg
1234 5/14/2014 10:02:50 5/14/2014 10:02:52 5/14/2014 abc start success
1234 5/14/2014 10:09:26 5/14/2014 10:09:32 5/14/2014 abc start success
1234 5/14/2014 11:45:11 5/14/2014 11:45:25 5/14/2014 abc start success
1234 5/14/2014 16:48:20 5/14/2014 16:48:22 5/14/2014 abc start success
1234 5/14/2014 19:02:52 5/14/2014 19:03:00 5/14/2014 abc start success
5678 5/14/2014 22:02:52 5/14/2014 22:03:00 5/14/2014 pqr start success
5678 5/14/2014 23:10:40 null 5/14/2014 pqr start success
我正在寻找Max of start的消息与成功的消息配对。
我试图在这两个表的时间戳之间得到最接近的匹配。 以下是另一种查看数据的方法。 要满足的条件:一个启动消息可以具有多个成功消息,其具有相同或不同的时间戳,用于相同的id和jobid。我需要选择最大开始消息到成功消息的最小消息,以使其匹配。
id Time_stamp_message_recieved message jobid
1234 5/14/2014 10:02:29 started abc
1234 5/14/2014 10:02:29 started abc
1234 5/14/2014 10:02:29 started abc
1234 5/14/2014 10:02:29 started abc
1234 5/14/2014 10:02:31 successful abc
1234 5/14/2014 10:02:31 successful abc
1234 5/14/2014 10:02:31 successful abc
1234 5/14/2014 10:02:31 successful abc
1234 5/14/2014 10:09:06 started abc
1234 5/14/2014 10:09:06 started abc
1234 5/14/2014 10:09:06 started abc
1234 5/14/2014 10:09:06 started abc
1234 5/14/2014 10:09:10 successful abc
1234 5/14/2014 10:09:10 successful abc
1234 5/14/2014 10:09:10 successful abc
1234 5/14/2014 10:09:10 successful abc
1234 5/14/2014 11:45:11 started abc
1234 5/14/2014 11:45:11 started abc
1234 5/14/2014 11:45:11 started abc
1234 5/14/2014 11:45:11 started abc
1234 5/14/2014 16:48:22 successful abc
1234 5/14/2014 19:02:52 started abc
1234 5/14/2014 19:03:00 successful abc
答案 0 :(得分:0)
您的查询是:
select a.timestamp_for_start_message, b.timestamp_for_start_message
from start_message a join
success_message b
where a.id1 = b.id1 and a.jobid = b.jobid;
当然这会产生重复。示例数据中的每一行都具有id
和jobid
的相同值。
时间戳似乎也是一个连接键。你似乎在寻找平等,所以你可以这样做:
select a.timestamp_for_start_message, b.timestamp_for_start_message
from start_message a join
success_message b
where a.id1 = b.id1 and a.jobid = b.jobid and
a.TIMESTAMP_for_start_message = b.TIMESTAMP_for_success_message
答案 1 :(得分:0)
使用最新的样本数据,显然仍然可以达到目的:
SELECT
DISTINCT
StMT.id1
, TIMESTAMP_for_start_message
, (SELECT MIN(TIMESTAMP_for_success_message)
FROM SuccessMessageTable
WHERE
(id1 = StMT.id1)
AND (someDate = StMT.someDate)
AND (jobID = StMT.jobID)
AND (TIMESTAMP_for_success_message >= StMT.TIMESTAMP_for_start_message)) TIMESTAMP_for_success_message
, StMT.someDate
, StMT.jobID
, NULL start_message
FROM StartMessageTable StMT
ORDER BY id1, jobID, TIMESTAMP_for_start_message;