Solved

Create a pseudo-pivot query for a report

Posted on 2013-11-13
2
271 Views
Last Modified: 2013-11-14
Hello,

I have table with widgets and a table with tasks.  Each widget has one of each of the tasks, none get left out.

The tables look something like so:

tblWidget
Widget_ID INT

tblTask
Task_ID CHAR(5)
Task_Name VARCHAR(50)

tblWorkflow
Widget_ID INT
Task_ID CHAR(5)
Completion_Date DATETIME

Currently I can run a query on tblWorkflow and see my data like so:
Widget ID          Task ID           Completion_Date
1                         1                     1/1/13
1                         2                     1/2/13
2                         1                     3/1/13
2                         2                     3/2/13
...etc...

However, I'd like to create a query that pivots the data so all my task names are listed horizontally across the top and my widgets are listed vertically on the side like so:
Widget ID          Task #1          Task #2          Task #3
1                         1/1/13            1/2/13            1/3/13
2                         3/1/13            3/2/13            3/3/13
...etc...

The results should  dynamically pick up any new tasks as they are added so they don't need to be added manually.

Any thoughts?

Thanks in advance.
0
Comment
Question by:Torrwin
2 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 39647016
You will need to construct the PIVOT query dynamically, like this:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX)

SET @cols = STUFF((SELECT distinct ',' + QUOTENAME(c.Task_ID) 
            FROM tblWorkflow c
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT Widget_ID, ' + @cols + ' from 
            (
                select Widget_ID
                    , Completion_Date
                    , Task_ID
                from tblWorkflow
           ) x
            pivot 
            (
                 max(Completion_Date)
                for Task_ID in (' + @cols + ')
            ) p '
execute(@query)

Open in new window

0
 
LVL 13

Author Closing Comment

by:Torrwin
ID: 39648193
You are a genius, thanks so much!
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now