SQL多表查询指导

时间:2013-10-29 11:01:00

标签: sql sql-server-2008-r2

我有以下查询:

SELECT
    _RES_COLL_EVM00012.MachineID,
    _RES_COLL_EVM00012.Name,
    v_GS_NETWORK_ADAPTER_CONFIGUR.IPAddress0,
    v_GS_NETWORK_ADAPTER_CONFIGUR.DefaultIPGateway0,
    v_GS_NETWORK_ADAPTER_CONFIGUR.TimeStamp,
    v_GS_NETWORK_ADAPTER_CONFIGUR.RevisionID
FROM
    _RES_COLL_EVM00012
    LEFT JOIN v_GS_NETWORK_ADAPTER_CONFIGUR
    ON _RES_COLL_EVM00012.MachineID = v_GS_NETWORK_ADAPTER_CONFIGUR.ResourceID
WHERE
    v_GS_NETWORK_ADAPTER_CONFIGUR.IPEnabled0 = 1
    AND v_GS_NETWORK_ADAPTER_CONFIGUR.IPAddress0 != '0.0.0.0'
    AND v_GS_NETWORK_ADAPTER_CONFIGUR.IPAddress0 IS NOT NULL
    AND v_GS_NETWORK_ADAPTER_CONFIGUR.DefaultIPGateway0 != '0.0.0.0'
    AND v_GS_NETWORK_ADAPTER_CONFIGUR.DefaultIPGateway0 IS NOT NULL
ORDER BY
    _RES_COLL_EVM00012.Name ASC,
    v_GS_NETWORK_ADAPTER_CONFIGUR.TimeStamp DESC, 
    v_GS_NETWORK_ADAPTER_CONFIGUR.RevisionID DESC

返回如下内容:

MachineID  Name     IPAddress0       DefaultGatewayIP0          TimeStamp   RevisionID
16777323   CTNB21   192.168.17.134   192.168.17.254    9/09/2013 13:07:11    8
16777323   CTNB21   192.168.17.143   192.168.17.254    9/09/2013 13:07:11    6
16777585   CTNB26   192.168.16.106   192.168.16.254   28/10/2013 22:39:55   33
16777585   CTNB26   192.168.16.116   192.168.16.254   28/10/2013 22:39:55   27

显然,ResourceID在表v_GS_NETWORK_ADAPTER_CONFIGUR中不是唯一的。我需要做的是显示表_RES_COLL_EVM00012中的每一行以及来自v_GS_NETWORK_ADAPTER_CONFIGUR的每行的SINGLE行。

从v_GS_NETWORK_ADAPTER_CONFIGUR中选择的行应该是具有最新TimeStamp和最大版本ID的行。

另请注意,我实际上并不想选择MachineID,TimeStamp或RevisionID,我刚刚这样做是为了更好地解释我的请求。

还有一件事,如果v_GS_NETWORK_ADAPTER_CONFIGUR中不存在与MachineID / ResourceID匹配的行,我仍然需要输出名称但是包含IPAddress0和DefaultGatewayIP0的空白值

因此,为了澄清,我希望示例结果集看起来像这样:

Name      IPAddress0        DefaultGatewayIP0
CTNB21    192.168.17.134    192.168.17.254
CTNB26    192.168.16.106    192.168.16.254

2 个答案:

答案 0 :(得分:0)

在select语句中使用DENSE_RANK()OVER(PARTITION BY RevisionID,TimeStamp ORDER BY RevisionID,TimeStamp DESC),如下所示。

SELECT * 
FROM (SELECT _RES_COLL_EVM00012.MachineID,
             _RES_COLL_EVM00012.Name,
             v_GS_NETWORK_ADAPTER_CONFIGUR.IPAddress0,
             v_GS_NETWORK_ADAPTER_CONFIGUR.DefaultIPGateway0,
             v_GS_NETWORK_ADAPTER_CONFIGUR.TimeStamp,
             v_GS_NETWORK_ADAPTER_CONFIGUR.RevisionID,
             DENSE_RANK() OVER (PARTITION BY RevisionID, TimeStamp 
                                ORDER BY RevisionID, TimeStamp DESC) RowID
      FROM _RES_COLL_EVM00012
      LEFT JOIN v_GS_NETWORK_ADAPTER_CONFIGUR
             ON _RES_COLL_EVM00012.MachineID = v_GS_NETWORK_ADAPTER_CONFIGUR.ResourceID
     WHERE v_GS_NETWORK_ADAPTER_CONFIGUR.IPEnabled0 = 1
           AND v_GS_NETWORK_ADAPTER_CONFIGUR.IPAddress0 != '0.0.0.0'
           AND v_GS_NETWORK_ADAPTER_CONFIGUR.IPAddress0 IS NOT NULL
           AND v_GS_NETWORK_ADAPTER_CONFIGUR.DefaultIPGateway0 != '0.0.0.0'
           AND v_GS_NETWORK_ADAPTER_CONFIGUR.DefaultIPGateway0 IS NOT NULL
     ) XYZ
WHERE XYZ.RowID = 1

有关SQL Server的更多文章,请访问SQL Server Basics

答案 1 :(得分:0)

试试这个:

SELECT
    --_RES_COLL_EVM00012.MachineID,
    _RES_COLL_EVM00012.Name,
    ISNULL(v_GS_NETWORK_ADAPTER_CONFIGUR.IPAddress0,'') as IPAddress0,
    ISNULL(v_GS_NETWORK_ADAPTER_CONFIGUR.DefaultIPGateway0,'') as DefaultIPGateway0
    --v_GS_NETWORK_ADAPTER_CONFIGUR.TimeStamp,
    --v_GS_NETWORK_ADAPTER_CONFIGUR.RevisionID
FROM
    _RES_COLL_EVM00012
    LEFT JOIN v_GS_NETWORK_ADAPTER_CONFIGUR
    ON _RES_COLL_EVM00012.MachineID = v_GS_NETWORK_ADAPTER_CONFIGUR.ResourceID
    LEFT JOIN (SELECT a.ResourceID,a.RevisionID, MAX(a.TimeStamp) as TimeStamp
                FROM v_GS_NETWORK_ADAPTER_CONFIGUR a
                    join (SELECT ResourceID, MAX(RevisionID) as RevisionID
                        FROM v_GS_NETWORK_ADAPTER_CONFIGUR
                        GROUP BY ResourceID) b
                    ON a.ResourceID=b.ResourceID
                                  GROUP BY a.ResourceID,a.RevisionID
                )c
    ON v_GS_NETWORK_ADAPTER_CONFIGUR.ResourceID=c.ResourceID
        AND v_GS_NETWORK_ADAPTER_CONFIGUR.RevisionID=c.RevisionID
        AND v_GS_NETWORK_ADAPTER_CONFIGUR.TimeStamp=c.TimeStamp
WHERE
    c.RevisionID IS NOT NULL
ORDER BY
    _RES_COLL_EVM00012.Name ASC,
    v_GS_NETWORK_ADAPTER_CONFIGUR.TimeStamp DESC, 
    v_GS_NETWORK_ADAPTER_CONFIGUR.RevisionID DESC