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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:41:49+00:00 2026-05-20T10:41:49+00:00

In SQL Server 2008, I have a table for tracking the status history of

  • 0

In SQL Server 2008,

I have a table for tracking the status history of actions (STATUS_HISTORY) that has three columns ([ACTION_ID],[STATUS],[STATUS_DATE]).

Each ACTION_ID can have a variable number of statuses and status dates.

I need to convert these rows into columns that preferably look something like this:

[ACTION_ID], [STATUS_1], [STATUS_2], [STATUS_3], [DATE_1], [DATE_2], [DATE_3]

Where the total number of status columns and date columns is unknown, and – of course – DATE_1 correlates to STATUS_1, etc. And I’d like for the status to be in chronological order (STATUS_1 has the earliest date, etc.)

My reason for doing this is so I can put the 10 most recent Statuses on a report in an Access ADP, along with other information for each action. Using a subreport with each status in a new row would cause the report to be far too large.

Is there a way to do this using PIVOT? I don’t want to use the date or the status as a column heading.

Is it possible at all?

I have no idea where to even begin. It’s making my head hurt.

  • 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-20T10:41:50+00:00Added an answer on May 20, 2026 at 10:41 am

    Let us suppose for brevity that you only want 3 most recent statuses for each action_id (like in your example).

    Then this query using CTE should do the job:

    WITH rownrs AS
    (
     SELECT
       action_id
      ,status
      ,status_date
      ,ROW_NUMBER() OVER (PARTITION BY action_id ORDER BY status_date DESC) AS rownr
     FROM
      status_history
    )
    
    SELECT
      s1.action_id AS action_id
    
     ,s1.status AS status_1
     ,s2.status AS status_2
     ,s3.status AS status_3
    
     ,s1.status_date AS date_1
     ,s2.status_date AS date_2
     ,s3.status_date AS date_3
    
    FROM
    
    (SELECT * FROM rownrs WHERE rownr=1) AS s1
    
     LEFT JOIN
    (SELECT * FROM rownrs WHERE rownr=2) AS s2
     ON s1.action_id = s2.action_id
    
     LEFT JOIN
    (SELECT * FROM rownrs WHERE rownr=3) AS s3
     ON s1.action_id = s3.action_id
    

    NULL values will appear in the rows where the action_id has less then 3 status-es.

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

Sidebar

Related Questions

I have a couple of tables in a SQL 2008 server that I need
I have tried SQL Server 2008 Management Studio and other third party tools to
I'm using SQL Server 2008 Management studio viewing a 2005 server and have just
I have looked at the SQL Server 2008 feature comparison matrix and it lists
I have two named instances of SQL Server 2008 and am trying to set
anyone have script or procedures to install SQL Server 2008 Express, set up the
I am looking for SQL Server Profiler in SQL Server 2008 express edition.I have
SQL Server (2005/2008) Each of the below statements have the same result. Does anyone
Our database server is a SQL 2008 server. My colleagues all have XP service
I have a WinForm application built with VS 2008 (C#) and SQL Server Express

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.