我目前正在使用union
运算符从两个表中选择材料:
SELECT Material
FROM (
SELECT Material
FROM DP_Historical_Data
EXCEPT SELECT Material
FROM SAS_GLOBE_Material_to_SASBaseItem
) HD
UNION SELECT Material
FROM (
SELECT Material
FROM DP_Historical_Data_Archive
EXCEPT SELECT Material
FROM SAS_GLOBE_Material_to_SASBaseItem
) HDA
我现在要做的是使用检索到的材料从第三个表中获取描述,即:
Select MaterialDescription
from Description_Table DT
Where DT.Material = UnionResult.Material
这有什么整洁的方法吗?
答案 0 :(得分:1)
Select UnionResult.Material, DT.MaterialDescription
FROM Description_Table DT
JOIN
(
SELECT Material FROM
(
SELECT Material
FROM DP_Historical_Data
EXCEPT
SELECT Material
FROM SAS_GLOBE_Material_to_SASBaseItem
) HD
UNION
SELECT Material
FROM
(
SELECT Material
FROM DP_Historical_Data_Archive
EXCEPT
SELECT Material
FROM SAS_GLOBE_Material_to_SASBaseItem
) HDA
) UnionResult ON DT.Material = UnionResult.Material
答案 1 :(得分:0)
SELECT DT.MaterialDescription
FROM Description_Table DT
INNER JOIN (
SELECT Material
FROM (
SELECT Material
FROM DP_Historical_Data
EXCEPT
SELECT Material
FROM SAS_GLOBE_Material_to_SASBaseItem
) HD
UNION
SELECT Material
FROM (
SELECT Material
FROM DP_Historical_Data_Archive
EXCEPT
SELECT Material
FROM SAS_GLOBE_Material_to_SASBaseItem
) HDA
) unires
WHERE DT.Material = unires.Material
答案 2 :(得分:0)
CREATE VIEW UnionResult AS
(
SELECT Material FROM (SELECT Material FROM DP_Historical_Data EXCEPT SELECT Material
FROM SAS_GLOBE_Material_to_SASBaseItem) HD
UNION
SELECT Material FROM
(SELECT Material FROM DP_Historical_Data_Archive EXCEPT SELECT Material FROM
SAS_GLOBE_Material_to_SASBaseItem) HDA
)
Select MaterialDescription from Description_Table DT,UnionResult
Where DT.Material = UnionResult.Material