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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:08:11+00:00 2026-05-30T01:08:11+00:00

I have two tables Employee and EmployeeLog (with the same columns except LogCreatedDate )

  • 0

I have two tables Employee and EmployeeLog (with the same columns except LogCreatedDate)

Columns are something like this:

EmployeeID, EmployeeName, LastModifiedBy, ModifiedDate,
ColumnA, ColumnB, ColumnC bla-bla-bla

I need T-SQL which give me output as shown in this table.

ResultSet 1:

enter image description here

On every Employee table update I insert a new row in EmployeeLog table with current values.

EmployeeLog table contains multiple rows for each EmployeeID so I need previous and current value in column that is modified.

So my first step is that I fetch TOP 2 record order by LogCreatedDate DESC. Now what should I do for getting output as shown in table?

ResultSet 2:
enter image description here

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-30T01:08:12+00:00Added an answer on May 30, 2026 at 1:08 am

    Try this:

    DECLARE @EmployeeLog TABLE
    (
        EmployeeID int,
        EmployeeName varchar(50),
        LastModifiedBy varchar(50),
        ModifiedDate datetime,
        ColumnA varchar(50),
        ColumnB varchar(50),
        ColumnC varchar(50),
        LogCreatedDate datetime
    )
    
    INSERT INTO @EmployeeLog VALUES (1, 'A', 'A', '2012-02-07', 'A', 'B', 'C', '2012-02-07')
    INSERT INTO @EmployeeLog VALUES (1, 'B', 'B', '2012-02-06', 'AA', 'BB', 'CC', '2012-02-06')
    
    ;
    WITH    Basics AS
    (
            SELECT  *, ROW_NUMBER() OVER (ORDER BY LogCreatedDate DESC) as Entry
            FROM    @EmployeeLog 
    ),      PivotTable AS
    (
            SELECT  EmployeeID,
                    LastModifiedBy,
                    ModifiedDate,
                    'ColumnA' ColumnName,
                    ColumnA ColumnValue,
                    LogCreatedDate,
                    Entry
            FROM Basics
            UNION ALL
            SELECT  EmployeeID,
                    LastModifiedBy,
                    ModifiedDate,
                    'ColumnB' ColumnName,
                    ColumnB ColumnValue,
                    LogCreatedDate,
                    Entry
            FROM Basics
            UNION ALL
            SELECT  EmployeeID,
                    LastModifiedBy,
                    ModifiedDate,
                    'ColumnC' ColumnName,
                    ColumnC ColumnValue,
                    LogCreatedDate,
                    Entry
            FROM Basics
    )
    SELECT  [Current].LastModifiedBy, [Current].ModifiedDate,
            Previous.EmployeeID, Previous.ColumnName,
            Previous.ColumnValue [Previous Value],
            [Current].ColumnValue [Current Value]
    FROM    PivotTable Previous
    JOIN    PivotTable [Current]
        ON  Previous.EmployeeID = [Current].EmployeeID AND
            Previous.ColumnName = [Current].ColumnName AND
            Previous.Entry = [Current].Entry + 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables one Employee and mailing Subscriptions Employee looks like this: Name
So my situation is this. I have two tables, e.g. employee and department. When
I have two tables kinda like this: // Person.Details Table: PersonID int [PK] |
Hypothetically I have two tables Employee and Locations. Additionaly I have a view viewEmpLocation
I have two tables: a schedule table that contains information about how an employee
I have two tables, one Company and one Employee: class Company(models.Model): name = models.CharField(max_length=60)
I have two tables, both named as say, Employee in two different schema HR
Suppose I have a dataset with those two immortal tables: Employee & Order Emp
I have two tables Employee ID | Name | Department ---------------------- 121 |Name1 |
I have two tables: employee with fields employee_id, firstname, middlename, lastname timecard with fields

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.