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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:51:18+00:00 2026-06-03T13:51:18+00:00

That is my table #EmployeeTemp . What I would like to do is retrieving

  • 0

My Current in used Table

That is my table #EmployeeTemp.

What I would like to do is retrieving data like that below.

To display like that style

So first of all, I use below query to retrieve data as I want.

CREATE TABLE #EmployeeTemp
(Employee_Name VARCHAR(50),
Confirmed_Date DATETIME,
Field_Type VARCHAR(50),
Field_Value VARCHAR(50))

INSERT INTO #EmployeeTemp VALUES ('John', '2009/1/1', 'Title', 'Programmer');
INSERT INTO #EmployeeTemp VALUES ('John', '2009/1/1', 'Department', 'IT Department');
INSERT INTO #EmployeeTemp VALUES ('John', '2011/1/1', 'Title', 'Sr. Programmer');
INSERT INTO #EmployeeTemp VALUES ('David', '2010/1/1', 'Title', 'Software Engineer');
INSERT INTO #EmployeeTemp VALUES ('David', '2010/1/1', 'Department', 'IT Department');

SELECT * FROM #EmployeeTemp

SELECT 
MyEmployee.Confirmed_Date,
MyEmployee.Employee_Name,   
Title = MAX(MyEmployee.Field_Value),
Department = (SELECT 
                    Field_Value
                FROM #EmployeeTemp
                WHERE Field_Type = 'Department'
                AND Employee_Name = MyEmployee.Employee_Name
                GROUP BY Employee_Name, Field_Value)
FROM #EmployeeTemp AS MyEmployee
WHERE MyEmployee.Confirmed_Date IN (SELECT MAX(Confirmed_Date) FROM #EmployeeTemp GROUP BY Employee_Name)
GROUP BY MyEmployee.Employee_Name, MyEmployee.Confirmed_Date
ORDER BY MyEmployee.Confirmed_Date DESC

It worked as I wanted. But what I would like to ask is could you please give me any suggestion to write query more professional way.

Every suggestion will be appreciated.

I am using Microsoft SQL Server 2008 version.

  • 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-03T13:51:19+00:00Added an answer on June 3, 2026 at 1:51 pm

    This question is very subjective, so what in my opinion is a more elegant solution, may be a less elegant solution in someone else’s opinion. However, this is the approach I would adopt, using the SQL-Server PIVOT Functionality.

    ;WITH Employees AS
    (   SELECT  *, 
                MAX(Confirmed_Date) OVER(PARTITION BY Employee_Name, Field_Type) [MaxFieldConfirmedDate],
                MAX(Confirmed_Date) OVER(PARTITION BY Employee_Name) [MaxConfirmedDate]
        FROM    #EmployeeTemp
    )
    SELECT  *
    FROM    (   SELECT  MaxConfirmedDate [Confirmed_Date], Employee_name, Field_Type, Field_Value
                FROM    Employees
                WHERE   Confirmed_Date = MaxFieldConfirmedDate
            ) data
            PIVOT
            (   MAX(Field_Value)
                FOR Field_Type IN ([Title], [Department])
            ) Pvt
    

    The reason I prefer this approach is firstly it is less work on the server (when checking actual execution plans for your query compared to this one, this one had a cost of 27% relative to the batch, as opposed to the 73% cost of yours). Secondly, if you need to add more fields, it is simply a case of extending the following line:

    FOR Field_Type IN ([Title], [Department])
    

    e.g. with the following extra data:

    INSERT INTO #EmployeeTemp VALUES ('John', '2011/1/1', 'Test', 'Test');
    INSERT INTO #EmployeeTemp VALUES ('David', '2010/1/1', 'Test', 'Test');
    

    you would only need to make the line:

    FOR Field_Type IN ([Title], [Department], [Test])
    

    to get the extra column required in the output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with a primary key that I would like to add
I have a query that outputs the resulting data into a table. I have
My table is posting and in that table, it shows all the posting people
i have created that table on my sql server : create table Empolyee( ESSN
I know that table sources need a data source to hold the data that
I want to make a table view in my iPhone Application.. In that table
I have window that contains a table on screen, now I want to attach
I have a table with structure like that: table name: shop id_shop int(10) name
I need one cell that will contains sum of all items from another table.
I have a table that contains a lot of data, where we particularly care

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.