SQL:Using 根据单独的字符串列表检查值是否存在
SQL:Using group by to check existence of a value against a separate list of strings
我正在尝试按 OrderID 和 lineID 进行分组,return 仅从包含单独字符串列表中的任何值的组中的唯一 orderID 和 lineID 进行分组。
这是一些样本数据。
OrderID lineID positionID fieldOfInterest somefield1 somefield2 ....
1A2 10248 1 '10xa' - -
1A2 10248 2 '42-vf' - -
1A2 10248 3 '10xb' - -
1A2 10249 2 '10xa' - -
1C3 11200 5 '10002' - -
1C3 10250 1 'N52O' - -
1V8 10250 7 'fas01' - -
1V8 10250 8 '10002' - -
. . . . . .
. . . . . .
. . . . . .
separate list to search groups against: ('10xa','10002')
我正在寻找的结果是
OrderID lineID
1A2 10248 <------ group contained '10xa'
1A2 10249 <------ group contained '10xa'
1C3 11200 <------ group contained '10002'
1V8 10250 <------ group contained '10002'
我试过了
having (min(fieldOfInterest) in ('10xa','10002')) or (max(fieldOfInterest) in ('10xa','10002'))
但我认为这不会一直有效。
也许我可以在字符串列表和 table.
上进行某种连接
在 Postgres 中,我会推荐聚合和 bool_or()
:
select orderID, lineID
from mytable
group by orderiD, lineID
having bool_or(fieldOfInterest in ('10xa','10002'))
我正在尝试按 OrderID 和 lineID 进行分组,return 仅从包含单独字符串列表中的任何值的组中的唯一 orderID 和 lineID 进行分组。
这是一些样本数据。
OrderID lineID positionID fieldOfInterest somefield1 somefield2 ....
1A2 10248 1 '10xa' - -
1A2 10248 2 '42-vf' - -
1A2 10248 3 '10xb' - -
1A2 10249 2 '10xa' - -
1C3 11200 5 '10002' - -
1C3 10250 1 'N52O' - -
1V8 10250 7 'fas01' - -
1V8 10250 8 '10002' - -
. . . . . .
. . . . . .
. . . . . .
separate list to search groups against: ('10xa','10002')
我正在寻找的结果是
OrderID lineID
1A2 10248 <------ group contained '10xa'
1A2 10249 <------ group contained '10xa'
1C3 11200 <------ group contained '10002'
1V8 10250 <------ group contained '10002'
我试过了
having (min(fieldOfInterest) in ('10xa','10002')) or (max(fieldOfInterest) in ('10xa','10002'))
但我认为这不会一直有效。 也许我可以在字符串列表和 table.
上进行某种连接在 Postgres 中,我会推荐聚合和 bool_or()
:
select orderID, lineID
from mytable
group by orderiD, lineID
having bool_or(fieldOfInterest in ('10xa','10002'))