Solved

Create a pseudo-pivot query for a report

Posted on 2013-11-13
2
273 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Optimizing a query 3 33
sql server query from excel 3 57
MS SQL Pivot table help 4 12
What query can i write to find where a function is 4 15
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

911 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

23 Experts available now in Live!

Get 1:1 Help Now