The below table has PC asset information and I need to remove slices of data from it based on different criteria.
I need to create a View in SQL Server 2005 which returns my results.
I tried to accomplish my goals using temporary tables until I realized that I could not use temporary tables in a View.
I then tried to use a CTE until I realized that deleting data from a CTE would also delete data from the actual table.
I cannot delete data from the actual table. I cannot create another table in the database either.
The table has 160,000 records.
The table:
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.
)
I will explain what I want to accomplish:
1) Read in all the information from the table dbo.dsm_hardware_basic. Lets call this: dsm_hardware_basic_copy.
2) Query dbo.dsm_hardware_basic and remove data that fits the following criteria from dsm_hardware_basic_copy.
This basically removes the duplicate [HostUUID] with the oldest [LastAgentExecution] time.:
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) Additionaly query dbo.dsm_hardware_basic and remove data that fits the following criteria from dsm_hardware_basic_copy:
This basically removes the duplicate [HostName] with the oldest [LastAgentExecution] time.:
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
I wasn’t sure how to do this in the above select, but not only should the COUNT of [HostName] be > 1, but [Name] should equal everything in [HostName] before the first period in [HostName]. Example [Name]: PCASSET001. Example [HostName]: PCASSET001.companydomain.com. I know this sounds strange considering the kind of PC data we are talking about in these two columns, but it is something I actually need to contend with.
3) Additionally query dbo.dsm_hardware_basic and remove data that fits the following criteria from dsm_hardware_basic_copy:
This basically removes the duplicate [Name] with the oldest [LastAgentExecution] time.:
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.
You’ve actually asked several different questions here and I’m not sure I completely follow the logic of the query, however, constructing it should not be too difficult.
To start with, you can work
dsm_hardware_basicdirectly rather than a copy:Now the part that
What we’ve done is partitioned your table by
HostUUIDsorted by newestLastAgentExecutionand removed every UUID from the query that matches our result using aJOIN.We can now apply the same logic to your
HostName:I’ll leave the final part to you as an exercise. Finally, after you’ve done debugging, just add
CREATE VIEWto this.