2011-12-15 48 views
0

對於給定的prcs_id,prcs_run_id,multicast_id,我需要multicast_message_recovery_processed中的端點不在multicast_endpoints表的端點列表中的消息的消息ID和端點。SQL中的新手。獲取消息和未處理的端點

數據 -

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還沒有達到destBdestD而消息2未達到destAdestCdestD

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

好吧,這看起來醜陋,有可能是一個更好的方式來寫這一點,但是,我認爲它的工作原理,並希望這將是一個起點,爲您提高:

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 
+0

感謝Bassam。你能幫我理解查詢嗎?首先,爲什麼需要嵌套塊中的交叉連接? – Narita 2011-12-16 05:08:33