0

使用 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
4

3 回答 3

4

您正在查找的查询可以简单地写成这样:

SELECT ComputerName, A.ComputerID, Name, Value FROM BESEXT.COMPUTER A
CROSS JOIN (SELECT * FROM BESEXT.ANALYSIS_PROPERTY WHERE ID BETWEEN 1672 AND 1674) B
LEFT JOIN BESEXT.ANALYSIS_PROPERTY_RESULT C ON A.ComputerId = C.ComputerId AND B.ID = C.PropertyId
ORDER BY ComputerId, Name

首先获取您关心的所有计算机属性组合:

SELECT * FROM BESEXT.COMPUTER A
CROSS JOIN (SELECT * FROM BESEXT.ANALYSIS_PROPERTY WHERE ID BETWEEN 1672 AND 1674) B

这会产生结果:

ID  ComputerId  ComputerName  ID    Name
--  ----------  ------------  --    ----
1   595640      PO121203866   1672  DisplayName
2   631459      PO121201739   1672  DisplayName
3   1101805     PO121201100   1672  DisplayName
1   595640      PO121203866   1673  SequenceName
2   631459      PO121201739   1673  SequenceName
3   1101805     PO121201100   1673  SequenceName
1   595640      PO121203866   1674  SequenceVersion
2   631459      PO121201739   1674  SequenceVersion
3   1101805     PO121201100   1674  SequenceVersion

从那里,您只需执行左连接BESEXT.ANALYSIS_PROPERTY_RESULT以获取您的值,并包含ORDER BY子句对其进行排序。

于 2015-02-23T00:18:40.827 回答
1

您可以通过使用交叉联接来设置所有计算机的属性,然后使用左联接来连接到设置了值的那些计算机的实际属性值来执行此操作:

SELECT * FROM (
    SELECT
       C.ComputerName,
       C.ComputerID,
       AP.Name,
       AP.ID
    FROM BESEXT.COMPUTER C
    CROSS JOIN BESEXT.ANALYSIS_PROPERTY AP
    WHERE AP.ID IN (1672, 1673, 1674)
) AP
LEFT JOIN BESEXT.ANALYSIS_PROPERTY_RESULT AR  
ON AP.ComputerID = AR.ComputerID AND AP.ID = AR.PropertyID
ORDER BY AP.ComputerName DESC, AP.Name
于 2015-02-23T00:19:32.180 回答
0

作为LOJ会是这样吗?

SELECT
    R.ComputerName,
    C.ComputerID,
    C.Name,
    C.Value
FROM BESEXT.COMPUTER R
-- LEFT OUTER JOIN ADDED HERE! 
LEFT OUTER JOIN 
(
    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)
) C 
  ON C.ComputerID = R.ComputerID
ORDER BY C.ComputerID, C.Name
于 2015-02-23T00:19:39.877 回答