您正在查找遺失的汽車文件。所以交叉加入汽車和文件類型,並尋找不在汽車表格中的組合。
select c.description as car, dt.description as doctype
from table_cars c
cross join table_documenttypes dt
where (c.id_car, dt.id_documenttype) not in
(
select cd.id_car, cd.id_documenttype
from table_cardocuments cd
);
更新:它顯示SQL Server的IN子句非常有限,無法處理值列表。但是NOT IN子句可以很容易地不可替代的存在:
select c.description as car, dt.description as doctype
from table_cars c
cross join table_documenttypes dt
where not exists
(
select *
from table_cardocuments cd
where cd.id_car = c.id_car
and cd.id_documenttype = dt.id_documenttype
);
更新:由於你是隻在特定id_documenttype(您不得不添加and dt.id_documenttype in (1, 2, 3, 4)
到查詢)感興趣,可以生成記錄它們在飛行中而不必讀取table_documenttypes。
爲了做到這一點取代
cross join table_documenttypes dt
與
cross join (values (1), (2), (3), (4)) as dt(id_documentType)
樣本數據和預期的結果幫助。你想要所有四個文件丟失嗎?或者,如果四個中的任何一個失蹤了,你想嗎? –
想要爲每輛車丟失的每個文件得到1行 –