我有一個表有兩列(p_id,id_type),其中p_id可以有多個類型。我需要找到沒有特定類型的p_ids。這可以更有效地完成
P_ID ID_TYPE
----------- -------------
12456 6
12456 7
56897 10
25686 9
25686 22
25686 7
56897 22
這是我用過的查詢,但想知道是否有更有效的方法來做到這一點。
select p_id
into #temp1
from table2
where id_type = 6
SELECT
distinct table2.p_id
,table1.NAME
,table1.TYPE
FROM
table2 left join table1
on table2.p_id = table1.p_id
where
table2.p_id not in
(select p_id from #temp1)
and type = 'XYZ'
預期結果應該是不具有ID_TYPE = 6
P_ID Name Type
56897 Smith Physician
25686 Jones Physician
您可以添加預期的結果嗎? – Jens 2014-09-10 19:09:13
你的代碼很好。你可以刪除臨時表的創建,並在你正在做的內部select中處理獲得帶有id_type = 6的p_ids,這是你已經在做的,但以更長的方式。 – SQLChao 2014-09-10 19:17:32
你是什麼意思'我需要找到沒有特定類型的p_ids.'? – KrazzyNefarious 2014-09-10 19:19:08