Solved

Create a pseudo-pivot query for a report

Posted on 2013-11-13
2
277 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 25

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
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.

728 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