Solved

Pivot Table

Posted on 2011-03-02
2
225 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Performance is the key factor for any successful data integration project, knowing the type of transformation that you’re using is the first step on optimizing the SSIS flow performance, by utilizing the correct transformation or the design alternat…
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

747 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

10 Experts available now in Live!

Get 1:1 Help Now