Solved

Pivot Table

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

 Watch the Recording: Learning MySQL 5.7

MySQL 5.7 has a lot of new features. If you've dabbled with an older version of MySQL, it is definitely worth learning.

Question has a verified solution.

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

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to shrink a transaction log file down to a reasonable size.
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.

636 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