对不起,这个没有用......可能我早些时候应该澄清一下。值A,B,C,D等......表中的CODE是不同的值。表中有数百个ID,每个ID可以包含一到多个Code值。在上面的示例中,假设表A中有5个不同的代码值。表A中有3个ID和每个ID,如下所示
ID代码 1 A. 1 B 1 C 2 D. 2 A. 3 B. 3 C. 4 A. 4 B 4 C 4 D. 4 E
如上所示,有几个ID与不同的代码值相关联。我需要如下结果
ID代码 1个D 1 E. 2 B. 2 C 2 E. 3 A. 3 D. 3 E
ID 4不应返回任何内容,因为它包含所有可能的代码(在本例中为A,B,C,D,E)
答案 0 :(得分:1)
首先,您应该在不同的子查询中为两个列采用不同的值,第二个交叉连接它们 - 这将为您提供所有可能的组合, 最后排除已经是presnet的组合
select *
from
(select distinct ID
from your_table) ytI, /* this sub-query will return all possible ID */
(select distinct code
from your_table) ytc /* this sub-query will return all possible code */
where (ytI.ID,ytc.Code) /* there will be cross-join as there are no join condition between first two tables*/
not in /* exclude those records which are already present */
(select id,code
from your_table yt_i)
答案 1 :(得分:0)
试试这个
select T2.ID, T1.missing_value
from
(
select 'A' missing_value from dual UNION
select 'B' from dual UNION
select 'C' from dual UNION
select 'D' from dual UNION
select 'E' from dual
) T1,
(
select distinct id from MYTABLE
) T2
WHERE NOT EXISTS
(
SELECT * FROM MYTABLE M WHERE M.CODE = T1.missing_value and M.ID = T2.ID
)
ORDER BY T2.ID, T1.missing_value