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

  • Home
  • SEARCH
  • 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 6561987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:39:21+00:00 2026-05-25T13:39:21+00:00

In an SQL Server 2008 database, I have tables such as: CREATE TABLE t_DeviceType

  • 0

In an SQL Server 2008 database, I have tables such as:

CREATE TABLE t_DeviceType
(
    ID INTEGER PRIMARY KEY IDENTITY, 
    Device VARCHAR(32) NOT NULL 
)

CREATE TABLE t_Device
(
    ID INTEGER PRIMARY KEY IDENTITY, 
    DeviceTypeID INTEGER NOT NULL,
    IPAddress INTEGER NOT NULL,
    RegTime DATETIME NOT NULL,
    IsActive BIT NOT NULL
)

Now I want a query that would pull the last registered, still active device (if any), for all device types.

For a single, given device, that’d be easy:

SELECT TOP 1 *
FROM t_Device
WHERE IsActive = 1
  AND DeviceTypeID = 42 -- For example
ORDER BY RegTime DESC

However, I need that for all possible valued of DeviceTypeID.

I thought I could do it with a JOIN statement, but there’s no way I found to limit the number of records pulled by a JOIN effectively.

I tried the following:

SELECT t_DeviceType.ID AS TypeID, 
       t_DeviceType.Device, 
       Device.ID AS DeviceID, 
       Device.IPAddress AS IPAddress
FROM t_DeviceType
JOIN ( 
    SELECT TOP 1 t_Device.ID, DeviceTypeID, IPAddress 
    FROM t_Device
    JOIN t_DeviceType ON DeviceTypeID = t_DeviceType.ID
    WHERE IsActive = 1
    ORDER BY RegTime DESC
) AS Device ON Device.ConsoleTypeID = t_DeviceType.ID

But that only returned the last registered devide of any type.

Anybody has a suggestion?

Thanks,

  • 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-05-25T13:39:21+00:00Added an answer on May 25, 2026 at 1:39 pm

    You want to get a list of Devices along with their associated maximum RegTime.

    SELECT DeviceTypeID, max(RegTime) as LatestRegTime
    FROM t_Device
    WHERE IsActive = 1
    GROUP BY DeviceTypeID
    

    Now you can use this as a derived table and join to it (instead of the select top 1 you tried):

    SELECT t_DeviceType.ID AS TypeID, 
           t_DeviceType.Device, 
           Device.ID AS DeviceID, 
           Device.IPAddress AS IPAddress
    FROM t_DeviceType
    JOIN ( 
        SELECT t_Device.ID, 
               IPAddress, 
               DeviceTypeID, 
               max(RegTime) as LatestRegTime
        FROM t_Device
        WHERE IsActive = 1
        GROUP BY t_Device.ID, IPAddress, DeviceTypeID
    ) AS Device ON Device.DeviceTypeID = t_DeviceType.ID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have SQL Server 2008 database with 2 tables: Table A has columns ID
SQL Server 2008 Database Question. I have 2 tables, for arguments sake called Customers
I have SQL Server 2008 Ent and OLTP database with two big tables. How
I have a table in a sql server 2008 database that contains bunch of
In my database running on SQL Server 2008 R2 I have a special table
I have an EMPLOYEE table in a SQL Server 2008 database which stores information
I have a SQL Server 2008 database with 2 tables. These tables are defined
I have an SQL Server 2008 database, which contains about 40 tables. Most of
I have a SQL Server 2008 database where all access to the underlying tables
I have a SQL Server 2008 database. This database has two related tables: Customer

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.