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 7709409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:51:17+00:00 2026-06-01T00:51:17+00:00

I`m new to sql and have been stuck on the following issue for almost

  • 0

I`m new to sql and have been stuck on the following issue for almost a day now:

I have two tables that I pull values from, devices and devices_LOG. I need to display all the devices_LOG entries where devices.status = ‘1’ AND I need them to be unique (ie I only want to see one devices_LOG entry for each device). My code looks like this:

    SELECT DISTINCT 
                     devices_LOG.device_id, MAX(devices_LOG.LogDate) AS LogDate,
                     manufacturers.name, devices_LOG.LogType, devices_LOG.userName, 
                     devices_LOG.userFullname, devices.invnumber, devices.modelname,
                     devices.modelnumber
    FROM             devices_LOG 
    INNER JOIN
                     devices ON devices_LOG.device_id = devices.id AND 
                     devices_LOG.device_id = devices.id 
    INNER JOIN
                     manufacturers ON devices.manufacturer_id = manufacturers.id
    WHERE            (devices.devicestatus = '1') AND (devices_LOG.LogType = 'Out')
    GROUP BY         devices_LOG.device_id, manufacturers.name, devices_LOG.LogType,
                     devices_LOG.userName, devices_LOG.userFullname, devices.invnumber, 
                     devices.modelname, devices.modelnumber
    ORDER BY         devices_LOG.device_id

which is great for returning only entries for things that have device.status = ‘1’, but it returns multiple log entries for something with the same device.id. So, the results of my query look like this

    device_id  LogDate       username    LogType    modelname   ...etc
    1          11/12/2011    foo         out        generic
    1          11/10/2011    world       out        generic
    2          9/10/2011     hello       out        generic3
    2          8/9/2011      bye         out        generic3

when I need it to look like this:

    device_id  LogDate       username    LogType    modelname   ...etc
    1          11/12/2011    foo         out        generic
    2          9/10/2011     hello       out        generic3

I tried using MAX on LogDate, tried grouping, select distinct, etc….but I just can`t figure it out. Any ideas?

I realize my sql statement is pretty ugly right now, probably because I have been trying everything I can think of up to this point with no luck, so any help would be greatly appreciated, 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-06-01T00:51:18+00:00Added an answer on June 1, 2026 at 12:51 am

    Just wrap the log entry in a CTE and use ROW_NUMBER with partition to get the data you want.

    For example if you wanted the oldest log entry, you could change the ORDER BY LogDate DESC into ASC.

    WITH Log AS (
    SELECT ROW_NUMBER() OVER (PARTITION BY device_id ORDER BY LogDate DESC) AS RN, *
    FROM Devices_LOG
    WHERE LogType = 'Out'
    )
    
    SELECT DISTINCT 
                         Log.device_id, Log.LogDate AS LogDate,
                         manufacturers.name, Log.LogType, Log.userName, 
                         Log.userFullname, devices.invnumber, devices.modelname,
                         devices.modelnumber
        FROM             devices 
        INNER JOIN
                         Log ON Log.device_id = devices.id AND 
                         Log.RN = 1 
        INNER JOIN
                         manufacturers ON devices.manufacturer_id = manufacturers.id
        WHERE            (devices.devicestatus = '1')
        ORDER BY         devices.device_id
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm stuck in this problem for quite while now. I have two tables ItemMaster
Hi I'm very new to sql but have been passed a job in which
I am new to SQL Server, and I have been tasked with setting permissions
I am pretty much new to using SQL Server 2000. I have been using
I have the following transaction: SQL inserts a 1 new record into a table
Tables have been created in our SQL Server 2008 R2 database on a local server,
We have a new SQL 2008 R2 instance that's part of a cluster (active/passive)
I'm a bit new to SQL and have trouble constructing a select statement. I
I have SQL server 2000 dev edition. But it isn't compatible with my new
I am totally new to SQL . I have a simple select query similar

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.