Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8885205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:15:05+00:00 2026-06-14T21:15:05+00:00

The below table has PC asset information and I need to remove slices of

  • 0

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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T21:15:07+00:00Added an answer on June 14, 2026 at 9:15 pm

    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_basic directly rather than a copy:

    SELECT 
        * 
    FROM dsm_hardware_basic
    

    Now the part that

    removes the duplicate [HostUUID] with the oldest [LastAgentExecution]
    time

    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
    

    What we’ve done is partitioned your table by HostUUID sorted by newest LastAgentExecution and removed every UUID from the query that matches our result using a JOIN.

    We can now apply the same logic to your 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
    

    I’ll leave the final part to you as an exercise. Finally, after you’ve done debugging, just add CREATE VIEW to this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to fetch Hierarchical/Tree data something like below from a Table which has
I have a table that has data like below(sorted by date) COL_A | COL_B
I have the below table which has 2 columns.I need to write a query
I have a report that I need to create from a UserHistory table in
I need some help on my mysql syntax. My table has user information but
the mysql table below has two columns. how do i select only those rows
I have a table which has the below columns. Ticket_id (Primary key, Int) Attachment1
I have a table as shown below. It has accounts of type Fixed and
I have 3 tables similar to the sctructure below CREATE TABLE [dbo].[EmpBasic]( [EmpID] [int]
PHP script running server side to handle queries from Java application. The table has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.