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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:12:39+00:00 2026-06-15T12:12:39+00:00

In SQL Server, I have two tables. One main table with all the data

  • 0

In SQL Server, I have two tables. One “main” table with all the data and a unique id per entry. The other table is an audit log, where that id from main will be stored multiple times in the audit table.

My query looks like:

SELECT 
    a.title, a.id, a.name, t.user, t.time  
FROM 
    MainTable a      
INNER JOIN 
    AuditLog AS t ON a.id = t.id    
WHERE 
    a.NAME LIKE 'Something%'  
    AND a.ACTIVE = 'Y' 

Which gives a result like:

TITLE           ID          NAME         USER          TIME
----------------------------------------------------------------
Something1      someth1     Some 1       User5         468534771
Something1      someth1     Some 1       User7         468574887
Something2      someth2     Some 2       User6         468584792

Which returns multiple results of the ID. I only want the oldest (from AuditLog) entry and not every one. So the result I would want looks like:

TITLE           ID          NAME         USER          TIME
----------------------------------------------------------------
Something1      someth1     Some 1       User5         468534771
Something2      someth2     Some 2       User6         468584792

How can this be done? I’m trying some subqueries within the join.

  • 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-15T12:12:40+00:00Added an answer on June 15, 2026 at 12:12 pm

    In sql server 2005+, you can use row_number()

    select title, id, name, user, time
    from
    (
      SELECT a.title, a.id, a.name, t.user, t.time,
        row_number() over(partition by a.id order by t.time) rn
      FROM MainTable a
      INNER JOIN AuditLog AS t
        ON a.id = t.id
      WHERE a.NAME LIKE 'Something%'
        AND a.ACTIVE='Y'
    ) src
    where rn = 1
    

    See SQL Fiddle with Demo

    Or you can use a sub-query to get the min time:

    SELECT a.title, a.id, a.name, l.user, l.time
    FROM MainTable a 
    INNER JOIN AuditLog l
      ON a.id = l.id
    INNER JOIN
    (
      select min(time) Mintime, id
      from AuditLog
      group by id
    ) AS t 
      ON l.id = t.id
      AND l.time = t.mintime
    WHERE a.NAME LIKE 'Something%' 
      AND a.ACTIVE='Y';
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In SQL Server 2008 I have two tables. One table for users: id_user -
Good Afternoon All, I have two tables in my SQL Server 2005 DB, Main
I have two tables in sql server 2008 R2. one table has records as
In SQL Server 2008: I have two tables, dtlScheme and dtlRenewal, with a one
I have two tables in Sql Server, one containing IDs for files and the
I have two sql server 2005 tables like so: Table A varName varStatus AGE
In MS SQL Server I have two tables [Products] and [Product_img] with relationship (ONE
I have two tables in SQL Server: Customer and Address Customer Table : CustomerID
We have two tables in a SQL Server 2005 database, A and B.There is
I came around some weird thing in MS SQL server. I have two tables

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.