Solved

Create a pseudo-pivot query for a report

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

860 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