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.
You can use a
PIVOTfor this type of query.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:
See SQL Fiddle with Demo