2

下表包含 PC 资产信息,我需要根据不同的标准从中删除数据片段。

我需要在 SQL Server 2005 中创建一个返回结果的视图。

我尝试使用临时表来实现我的目标,直到我意识到我不能在视图中使用临时表。

然后我尝试使用 CTE,直到我意识到从 CTE 中删除数据也会从实际表中删除数据。

我无法从实际表中删除数据。我也无法在数据库中创建另一个表。

该表有 160,000 条记录。

桌子:

TABLE dsm_hardware_basic
(
[UUID] binary(16) -- Randomly generated 16 digit key that is unique for each record, only column with no duplicate rows.
[HostUUID] binary(16) -- Randomly generated 16 digit key, column has duplicate rows.
[Name] nvarchar(255) -- Column that contains hostnames of computer assets. Example of record: PCASSET001. Column has duplicate rows.
[LastAgentExecution] datetime -- The last time that the software agent that collects asset information ran on the PC.
[HostName] nvarchar(255) -- The fully qualified domain name of the PC. Example of record: PCASSET001.companydomain.com. Column has duplicate rows.
)

我将解释我想要完成的事情:

1) 读入表 dbo.dsm_hardware_basic 中的所有信息。让我们称之为:dsm_hardware_basic_copy。

2) 查询 dbo.dsm_hardware_basic 并从 dsm_hardware_basic_copy 中删除符合以下条件的数据。这基本上删除了具有最早 [LastAgentExecution] 时间的重复 [HostUUID]。:

    SELECT   ,dsm_hardware_basic.[HostUUID]
             ,MIN(dsm_hardware_basic.[LastAgentExecution]) AS [LastAgentExecution]
    FROM      dsm_hardware_basic
    WHERE     dsm_hardware_basic.[HostUUID] <> ''
    GROUP BY  dsm_hardware_basic.[HostUUID]
              HAVING COUNT(*) = 2 -- The tiny amount of rows where this count is >2 will be left alone.

3) 另外查询 dbo.dsm_hardware_basic 并从 dsm_hardware_basic_copy 中删除符合以下条件的数据:这基本上删除了具有最早 [LastAgentExecution] 时间的重复 [HostName]。:

    SELECT   ,dsm_hardware_basic.[HostName]
             ,MIN(dsm_hardware_basic.[LastAgentExecution]) AS [LastAgentExecution]
    FROM      dsm_hardware_basic
    WHERE     dsm_hardware_basic.[HostName] <> ''
    GROUP BY  dsm_hardware_basic.[HostName]
              HAVING COUNT(*) > 1

我不确定如何在上面的选择中执行此操作,但不仅 [HostName] 的 COUNT 应该 > 1,而且 [Name] 应该等于 [HostName] 中第一个句点之前 [HostName] 中的所有内容。示例 [名称]:PCASSET001。示例 [主机名]:PCASSET001.companydomain.com。考虑到我们在这两列中讨论的 PC 数据类型,我知道这听起来很奇怪,但这是我真正需要解决的问题。

3) 另外查询 dbo.dsm_hardware_basic 并从 dsm_hardware_basic_copy 中删除符合以下条件的数据:

这基本上删除了具有最早 [LastAgentExecution] 时间的重复 [Name]。:

    SELECT   ,dsm_hardware_basic.[Name]
             ,MIN(dsm_hardware_basic.[LastAgentExecution]) AS [LastAgentExecution]
    FROM      dsm_hardware_basic
    WHERE     dsm_hardware_basic.[Name] <> ''
    GROUP BY  dsm_hardware_basic.[Name]
              HAVING COUNT(*) = 2 -- The tiny amount of rows where this count is >2 will be left alone.
4

1 回答 1

0

您实际上已经在这里问了几个不同的问题,我不确定我是否完全遵循查询的逻辑,但是,构建它应该不会太难。

首先,您可以dsm_hardware_basic直接工作而不是复制:

SELECT 
    * 
FROM dsm_hardware_basic

现在的部分

删除具有最早 [LastAgentExecution] 时间的重复 [HostUUID]

SELECT 
    dsm_hardware_basic.* 
FROM dsm_hardware_basic
INNER JOIN 
    (
        SELECT [UUID], ROW_NUMBER() OVER 
            (PARTITION BY [HostUUID] 
             ORDER BY [LastAgentExecution] DESC) AS host_UUID_rank
        FROM dsm_hardware_basic
        WHERE 
            [HostUUID] <> ''
    ) AS 
    duplicate_host_UUID_filtered ON dsm_hardware_basic.UUID = duplicate_host_UUID_filtered.UUID
    AND duplicate_host_UUID_filtered.host_UUID_rank = 1

我们所做的是HostUUID按最新排序对您的表进行分区,LastAgentExecution并使用JOIN.

我们现在可以将相同的逻辑应用于您的HostName

SELECT 
    dsm_hardware_basic.* 
FROM dsm_hardware_basic
INNER JOIN 
    (
        SELECT [UUID], ROW_NUMBER() OVER 
            (PARTITION BY [HostUUID] 
             ORDER BY [LastAgentExecution] DESC) AS host_UUID_rank
        FROM dsm_hardware_basic
        WHERE 
            [HostUUID] <> ''
    ) AS 
    duplicate_host_UUID_filtered ON dsm_hardware_basic.UUID = duplicate_host_UUID_filtered.UUID
    AND duplicate_host_UUID_filtered.host_UUID_rank = 1
INNER JOIN 
    (
        SELECT [UUID], ROW_NUMBER() OVER 
            (PARTITION BY [HostName] 
             ORDER BY [LastAgentExecution] DESC) AS host_UUID_rank
        FROM dsm_hardware_basic
        WHERE 
            [HostName] <> ''
    ) AS 
    duplicate_HostName_filtered ON dsm_hardware_basic.UUID = duplicate_HostName_filtered.UUID
    AND duplicate_HostName_filtered.host_UUID_rank = 1

我将把最后一部分留给你作为练习。最后,在您完成调试后,只需添加CREATE VIEW到此。

于 2012-11-26T04:09:50.893 回答