Solved

Pivot Table

Posted on 2011-03-02
2
246 Views
Last Modified: 2012-05-11
I need to create a pivot table, but I am not getting the results that I need.  I have the following data in a single table

QuestID       CustID       Results          Timestamp
--------------------------------------------------------------
Q1                      1            2/10/2004    2011-03-02 11:52am
Q2                      1            Yes               2011-03-02 11:52am
Q3                      1            Fridays         2011-03-02 11:52am
Q1                      2            2/16/2006    2011-03-02 1:52pm
Q2                      2            No                2011-03-02 1:52pm
Q3                      2            Sundays       2011-03-02 1:52pm

I need to pivot the QuestID, so the results would look more like this.

CustID          Q1            Q2               Q3          Timestamp (the timestamp is the same for all questions)
-------------------------------------------------------------------------------------------------------------
1              2/10/2004     Yes         Fridays       2011-03-02 11:52am
2              2/16/2060     No          Sundays     2011-03-02 1:52pm

Any help or direction would be greatly appreciated

Thanks,
0
Comment
Question by:clintnash
2 Comments
 
LVL 26

Accepted Solution

by:
tigin44 earned 500 total points
ID: 35021625
try this


SELECT CustID, [Q1], [Q2], [Q3], [Q4], Timestamp
FROM (
      select QuestID ,CustID ,Results,Timestamp
      from yourTable
      ) P
      PIVOT
      (
            MAX(Results) FOR QuestID IN ([Q1], [Q2], [Q3], [Q4])
      ) AS PVT
0
 
LVL 1

Author Closing Comment

by:clintnash
ID: 35022201
MAX(results) is what I was missing.  Thank you very much !!!
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

777 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