使用 SQL Server 2012 (LocalDB),我有三个表:
BESEXT.COMPUTER
BESEXT.ANALYSIS_PROPERTY
BESEXT.ANALYSIS_PROPERTY_RESULT
这些包含以下信息:
- BESEXT.COMPUTER:ComputerIDs 和 ComputerNames 之间的映射
- BESEXT.ANALYSIS_PROPERTY:可以映射到计算机的属性列表
- BESEXT.ANALYSIS_PROPERTY_RESULT:计算机属性值列表
首先,我执行以下查询:
SELECT
AR.ComputerID,
AP.Name,
AR.Value
FROM BESEXT.ANALYSIS_PROPERTY_RESULT AR
JOIN BESEXT.ANALYSIS_PROPERTY AP ON AP.ID = AR.PropertyID
AND AP.ID IN (1672, 1673, 1674)
ORDER BY AR.ComputerID, AP.Name
这会产生以下结果:
ComputerID Name Value
---------- ---- -----
595640 DisplayName Windows 8.1 x64 - Mobile Device Image - v3.2
595640 SequenceName Windows 8.1 x64 - Mobile Device Image
595640 SequenceVersion 3.2
631459 DisplayName Windows 8.1 x64 - Mobile Device Image - v3.2
631459 SequenceName Windows 8.1 x64 - Mobile Device Image
631459 SequenceVersion 3.2
在 BESEXT.COMPUTER 我有以下值:
ID ComputerID ComputerName
-- ---------- ------------
1 595640 PO121203866
2 631459 PO121201739
3 1101805 PO121201100
我想在第一次选择时执行我所有计算机对象的左外连接,这样我就知道哪些计算机我没有价值。
所以,首先我对前面的选择做一个简单的内部连接:
SELECT
C.ComputerName,
R.ComputerID,
R.Name,
R.Value
FROM (
SELECT
AR.ComputerID,
AP.Name,
AR.Value
FROM BESEXT.ANALYSIS_PROPERTY_RESULT AR
JOIN BESEXT.ANALYSIS_PROPERTY AP ON AP.ID = AR.PropertyID
AND AP.ID IN (1672, 1673, 1674)
) R
JOIN BESEXT.COMPUTER C ON C.ComputerID = R.ComputerID
ORDER BY R.ComputerID, R.Name
可以预见的是,这会产生以下结果集:
ComputerName ComputerID Name Value
------------ ---------- ---- -----
PO121203866 595640 DisplayName Windows 8.1 x64 - Mobile Device Image - v3.2
PO121203866 595640 SequenceName Windows 8.1 x64 - Mobile Device Image
PO121203866 595640 SequenceVersion 3.2
PO121201739 631459 DisplayName Windows 8.1 x64 - Mobile Device Image - v3.2
PO121201739 631459 SequenceName Windows 8.1 x64 - Mobile Device Image
PO121201739 631459 SequenceVersion 3.2
现在,对于大结局,让我们做LEFT OUTER JOIN:
SELECT
C.ComputerName,
R.ComputerID,
R.Name,
R.Value
FROM (
SELECT
AR.ComputerID,
AP.Name,
AR.Value
FROM BESEXT.ANALYSIS_PROPERTY_RESULT AR
JOIN BESEXT.ANALYSIS_PROPERTY AP ON AP.ID = AR.PropertyID
AND AP.ID IN (1672, 1673, 1674)
) R
-- LEFT OUTER JOIN ADDED HERE!
LEFT OUTER JOIN BESEXT.COMPUTER C ON C.ComputerID = R.ComputerID
ORDER BY R.ComputerID, R.Name
这会产生与内部连接完全相同的结果集!
这不是我想要的,也完全不是我所期待的。现在已经很晚了,我基本上是在做一些失眠的工作,但我认为这是尽可能简单的外部连接示例,对吧?我希望通过外部连接实现的结果是:
ComputerName ComputerID Name Value
PO121203866 595640 DisplayName Windows 8.1 x64 - Mobile Device Image - v3.2
PO121203866 595640 SequenceName Windows 8.1 x64 - Mobile Device Image
PO121203866 595640 SequenceVersion 3.2
PO121201739 631459 DisplayName Windows 8.1 x64 - Mobile Device Image - v3.2
PO121201739 631459 SequenceName Windows 8.1 x64 - Mobile Device Image
PO121201739 631459 SequenceVersion 3.2
PO121201100 NULL NULL NULL
PO121201100 NULL NULL NULL
PO121201100 NULL NULL NULL
PS:老实说,我正在寻找的结果更像是这样,但我觉得这将是一个完全不同的问题:
ComputerName Name Value
------------ ---- -----
PO121203866 DisplayName Windows 8.1 x64 - Mobile Device Image - v3.2
PO121203866 SequenceName Windows 8.1 x64 - Mobile Device Image
PO121203866 SequenceVersion 3.2
PO121201739 DisplayName Windows 8.1 x64 - Mobile Device Image - v3.2
PO121201739 SequenceName Windows 8.1 x64 - Mobile Device Image
PO121201739 SequenceVersion 3.2
PO121201100 DisplayName NULL
PO121201100 SequenceName NULL
PO121201100 SequenceVersion NULL