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 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'm using SQL Server 2008 and I have a table with three columns: Length
I have a table in a sql server 2008 database that contains bunch of
I have a table which contains my ads that can be searched in sql-server-2008.
I have a Microsoft SQL Server 2008 query that returns data from three tables
I have three table in SQL Server 2008: Students StudentId Name 1 Ghanshyam 2
I have a table stored on a SQL Server 2008, that associate a value
I have a table in SQL Server 2008 that looks kind of like this:
In SQL server 2008 I have table Theory with columns Thesis and Class. Thesis
System is: SQL Server 2008-R2 I have a table that one of its column
I have a table called XML (in SQL Server 2008) and it has a

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.