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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T02:49:18+00:00 2026-06-10T02:49:18+00:00

I have a table that looks like Customer_id Order_id Action Time_Stamp abc 123 placed

  • 0

I have a table that looks like

Customer_id     Order_id   Action    Time_Stamp
abc             123         placed     8/1/2012 10:00AM
abc             123         processed  8/1/2012 10:30AM
abc             123         shipped    8/1/2012 11:00AM

xyz             456         recieved   8/1/12 12:00PM 

what i am trying to get is

Customer ID     Order_id   Placed             Processed           Shipped            recieved
abc              123       8/1/2012 10:00Am    8/1/2012 10:30AM   8/1/2012 11:00AM   null
xyz              456       null                null               null               8/1/2012 12:00PM

i know the max number of columns i would need (the number of actions in other words) if that makes the query easier.

any help is much appreciated.

  • 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-10T02:49:19+00:00Added an answer on June 10, 2026 at 2:49 am

    You can use a PIVOT for this type of query.

    SELECT *
    FROM 
    (
        SELECT Customer_id, Order_id, Action, Time_Stamp
        FROM yourTable
    ) x
    PIVOT
    (
       max(Time_Stamp)
       for action in ([Placed], [Processed], [Shipped], [received])
    ) p
    

    See SQL Fiddle with Demo

    The above query works great if you know the values of the columns you want to create. But if you do not, then you can use a dynamic pivot:

    DECLARE @cols AS NVARCHAR(MAX),
        @query  AS NVARCHAR(MAX)
    
    select @cols = STUFF((SELECT distinct ',' + QUOTENAME(action) 
                        from test
                FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)') 
            ,1,1,'')
    
    
    set @query 
          = 'SELECT Customer_id, Order_id,' + @cols + ' from 
             (
                select Customer_id, Order_id, Action, Time_Stamp
                from test
             ) x
             pivot 
             (
                max(Time_Stamp)
                 for action in(' + @cols + ')
             ) p '
    
    execute(@query)
    

    See SQL Fiddle with Demo

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

Sidebar

Related Questions

I have a table that looks like: Client_ID | Order_ID | 10 | 1
I have data that looks like CUSTOMER, CUSTOMER_ID, PRODUCT ABC INC 1 XYX ABC
I have a table that looks like this: id value AGA 0.211 AGA 0.433
I have a table that looks like this: CREATE TABLE foobar ( id SERIAL
I have a table that looks like this: ID (pk,int) Col1 (nvarchar) Col2 (nvarchar)
I have a table that looks like this Id FirstName 5 Adam 6 Bob
I have a table that looks like this: table name: uno str_id | title
I have a table that looks like this Group Recipe Priority 0 A 400
I have a table that looks like this: nid vid tid 1 2 3
I have a table that looks like this: <table> <thead> <!-- random table headings

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.