对于给定的prcs_id,prcs_run_id,multicast_id,我需要来自multicast_message_recovery_processed端点的消息的消息ID和端点不在multicast_endpoints表的端点列表中。
数据 -
multicast_endpoints
prcs_id prcs_run_id multicast_id endpoint_id
100 1 multiX destA
100 1 multiX destB
100 1 multiX destC
100 1 multiX destD
100 1 multiY destX
100 1 multiY destY
multicast_message_recovery_processed
prcs_id prcs_run_id multicast_id message_id endpoint_id
100 1 multiX 1 destA
100 1 multiX 2 destB
100 1 multiX 1 destC
100 1 multiY 1 destX
对于给定的prcs_id = 100, prcs_run_id = 1, multicast_id = multiX
,查询应返回:
1 destB
1 destD
2 destA
2 destC
2 destD
这实际上意味着消息1未到达destB
和destD
,而消息2未到达destA
,destC
和destD
。
CREATE TABLE multicast_message_recovery_processed
(
prcs_id character varying NOT NULL,
prcs_run_id bigint NOT NULL,
multicast_id character varying NOT NULL,
message_id bigint,
endpoint_id character varying NOT NULL,
CONSTRAINT multicast_message_recovery_processed_pkey PRIMARY KEY (prcs_id , prcs_run_id , multicast_id , endpoint_id )
)
CREATE TABLE multicast_endpoints
(
prcs_id character varying NOT NULL,
prcs_run_id bigint NOT NULL,
multicast_id character varying NOT NULL,
endpoint_id character varying NOT NULL,
CONSTRAINT multicast_endpoints_pkey PRIMARY KEY (prcs_id , prcs_run_id , multicast_id , endpoint_id )
)
答案 0 :(得分:0)
好吧,这看起来很丑陋,而且可能有更好的方式来写这个,但是,我觉得它有效,希望它会成为你改进的起点:
SELECT DISTINCT p.message_id, e.endpoint_id AS endpoint_id
FROM dbo.multicast_endpoints e CROSS JOIN dbo.multicast_message_recovery_processed p
LEFT JOIN
(SELECT p.message_id, p.endpoint_id AS process_endpoint_id, e.endpoint_id AS endpoint_id
FROM dbo.multicast_endpoints e CROSS JOIN dbo.multicast_message_recovery_processed p
WHERE e.prcs_id = p.prcs_id AND e.prcs_run_id = p.prcs_run_id AND e.multicast_id = p.multicast_id
AND p.endpoint_id = e.endpoint_id
) t ON p.message_id = t.message_id AND e.endpoint_id = t.endpoint_id
WHERE e.prcs_id = p.prcs_id AND e.prcs_run_id = p.prcs_run_id AND e.multicast_id = p.multicast_id
AND e.prcs_id = '100' AND e.prcs_run_id = 1 AND e.multicast_id = 'MultiX' AND t.message_id IS NULL
ORDER BY message_id