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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:00:08+00:00 2026-05-27T23:00:08+00:00

We are using audit tables for each operational table, which stores the previous value

  • 0

We are using audit tables for each operational table, which stores the previous value of its operational equivalent plus change date, change type (UPDATE or DELETE) and its own auto incremental Primary Key.

So, for a table Users with columns UserID, Name, Email there would be a table xUsers with columns ID, OpererationType, OperationDate, UserID, Name, Email.

See that the xTable contains every column that its ‘parent’ does with 3 extra fields. This pattern is repeated for all tables used by our system.

table Users:

UserID           int    
Name             nvarchar
Email            nvarchar

table xUsers:

    xUserID          int    
    OpererationType  int
    OperationDate    datetime
    UserID           int    
    Name             nvarchar
    Email            nvarchar

Now, my question:
If I have a certain UserID, for which there is 2 entries in the xUsers table when the email was changed twice,

how would I construct a query that identifies which columns (can be more than 1) differ between the two rows in the audit table?

  • 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-27T23:00:09+00:00Added an answer on May 27, 2026 at 11:00 pm

    If I’m understanding this correctly, you’d like to create a query passing in the UserID as a parameter, which I’ll call @UserID for the following example.

    This query will select all rows from xUsers joined onto itself where there is a difference in a non-UserID column, using a series of case statements (one per column) to pull out specifically which columns differ.

    SELECT *
        , CASE 
            WHEN a.OperationType <> b.OperationType
            THEN 1
            ELSE 0
            END AS OperationTypeDiffers
        , CASE
            WHEN a.OperationDate <> b.OperationDate
            THEN 1
            ELSE 0
            END AS OperationDateDiffers
    FROM xUsers a
    JOIN xUsers b
        ON a.xUserID < b.xUserID
        AND a.UserID = b.UserID
        AND (a.OperationType <> b.OperationType
            OR a.OperationDate <> b.OperationDate) -- etc.
    WHERE a.UserID = @UserID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're using a 1 audit table for each monitored Table ; However, in our
I have an audit table which stores audit rows for a long running process
I have an audit table setup which essentially mirrors one of my tables along
I'm using SQL Server 2005. There is an audit trail table, containing over 1,000,000,000
I am using LINQ to SQL with single table inheritance for an audit log.
I have an entity model that has audit information on every table (50+ tables)
I am using an audit table to find what records a single destructive SQL
I have the following question, I am using audit tables for some entities in
I'm using triggers on my sql database to capture change information for a table,
I am using envers to audit my ParameterToValue entity. Its properties containerId, containerType, parameterId

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.