Solved

Pivot Table

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

Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

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

21 Experts available now in Live!

Get 1:1 Help Now