SELECT
b.ID
FROM
PLACE b
JOIN ART_TO_PLACE a ON b.ALIAS = a.PLACE_ALIAS
JOIN ART_TO_UNIT t ON t.MATL_TU_IDENT = :AUNITIDENT
WHERE
(
(a.GRP = t.GRP) --All GRPs must match, not just one
)
FETCH NEXT 1 ROWS ONLY
如果有一些a.GRP在ART_TO_UNIT中没有匹配项,或者如果有一些t.GRP在ART_TO_PLACE中没有匹配项,则不为其返回B.ID。查找下一个PLACE条目。
如何在没有动态SQL的情况下做到这一点?
更新
不好意思,这本来应该在问题里的,现在不删掉上面的部分了,因为已经有两个人回复了。
这里是MS SQL Server的最小可重复示例,因为我没有Oracle作为Playground:
CREATE TABLE WorkplaceCapabilities(
ID int IDENTITY(1,1) NOT NULL PRIMARY KEY,
WorkplaceID int NOT NULL,
AllowedType nvarchar(50) NOT NULL
)
INSERT INTO WorkplaceCapabilities (WorkplaceID, AllowedType) VALUES (1, 'Wood');
INSERT INTO WorkplaceCapabilities (WorkplaceID, AllowedType) VALUES (2, 'Gas');
INSERT INTO WorkplaceCapabilities (WorkplaceID, AllowedType) VALUES (2, 'Oil');
INSERT INTO WorkplaceCapabilities (WorkplaceID, AllowedType) VALUES (3, 'Steel');
INSERT INTO WorkplaceCapabilities (WorkplaceID, AllowedType) VALUES (3, 'Wood');
INSERT INTO WorkplaceCapabilities (WorkplaceID, AllowedType) VALUES (4, 'Gas');
INSERT INTO WorkplaceCapabilities (WorkplaceID, AllowedType) VALUES (4, 'Plastic');
INSERT INTO WorkplaceCapabilities (WorkplaceID, AllowedType) VALUES (4, 'Steel');
CREATE TABLE TransportUnitContents(
ID int IDENTITY(1,1) NOT NULL PRIMARY KEY,
UnitID int NOT NULL,
ContainedMaterial nvarchar(50) NOT NULL
)
INSERT INTO TransportUnitContents (UnitID, ContainedMaterial) VALUES (152, 'Wood');
INSERT INTO TransportUnitContents (UnitID, ContainedMaterial) VALUES (25, 'Oil');
INSERT INTO TransportUnitContents (UnitID, ContainedMaterial) VALUES (25, 'Gas');
INSERT INTO TransportUnitContents (UnitID, ContainedMaterial) VALUES (122, 'Wood');
INSERT INTO TransportUnitContents (UnitID, ContainedMaterial) VALUES (122, 'Steel');
INSERT INTO TransportUnitContents (UnitID, ContainedMaterial) VALUES (13, 'Gas');
INSERT INTO TransportUnitContents (UnitID, ContainedMaterial) VALUES (13, 'Steel');
INSERT INTO TransportUnitContents (UnitID, ContainedMaterial) VALUES (13, 'Plastic');
INSERT INTO TransportUnitContents (UnitID, ContainedMaterial) VALUES (99, 'Gas');
INSERT INTO TransportUnitContents (UnitID, ContainedMaterial) VALUES (111, 'Steel');
INSERT INTO TransportUnitContents (UnitID, ContainedMaterial) VALUES (111, 'Plastic');
现在我想有这样一个SQL查询,将返回
- 用于运输单元152 -工作场所1
- 用于运输单元25 -工作场所2
- 用于运输单元122 -工作场所3
- 用于运输单元13 -工作场所4
- 99 ----无工作场所
- 用于运输单元111 -无工作场所
2条答案
按热度按时间nhhxz33t1#
如果没有样本数据,很难回答,但看起来你想在
ART_TO_PLACE
和ART_TO_UNIT
之间使用FULL OUTER JOIN
,然后聚合并使用HAVING
子句来检查没有行被OUTER JOIN
ed(相反,所有行都匹配):或者可能是:
样本数据更新:
选项1:使用集合
定义集合数据类型:
然后,您可以将两个表的值收集到集合中,并在相同的集合值上进行连接:
选项2:比较计数
统计每个表中每个组的类型/材料数量,然后进行连接,并检查连接后的匹配数量是否相同:
对于示例数据,两个选项都将输出:
| 工作场所ID|UNITID|
| --------------|--------------|
| 1|一百五十二|
| 第二章|二十五|
| 三|一百二十二|
| 四|十三|
fiddle
6bc51xsx2#
如果我对你的问题理解正确的话,你可以像这样使用嵌套的exists条件: