我根据不同的“代码”ID分解数据;一个例子是:
ID Code_Value
1 100
1 200
1 300
1 600
1 700
2 300
2 600
2 700
3 400
3 600
3 800
etc.
我的目标是识别包含在两个不同列表中的Code_Value的所有ID组;所以,如果我要查找ID('100','300','500')内的代码以及('600','700','800')内的代码,它将返回ID值从上面的示例数据中得到1和2。
我找到了将这些列表合并到一个总体列表中的示例,并确定哪些ID在该总体列表中有一定数量的命中(https://dba.stackexchange.com/questions/95674/get-id-which-has-two-rows-with-different-values-for-one-column),但这对我没有好处,因为我需要ID到包含每个列表集的值。
我所做的一个示例尝试如下:
select id from my_table
where code_value in (<list_1>)
and code_value in (<list_2>)
group by id;
结果是我收到了每个列表中具有单独code_value的所有ID,而不是在每个列表中的整个ID上都有任何code_value。
非常感谢任何有关这方面的帮助, 设为unclesam
答案 0 :(得分:1)
您可以使用having
子句执行此操作:
select id
from t
group by id
having sum(case when code_value in (<list_1>) then 1 else 0 end) > 0 and
sum(case when code_value in (<list_2>) then 1 else 0 end) > 0 ;
having
子句中的每个条件都会计算每个列表的值数。 > 0
只是说至少有一个。