How to PIVOT Data

I am running the following query on a sql 2005 database

DECLARE @start DATETIME,
    @end DATETIME


        SET @start = DATEADD(day, -1, CONVERT(DATETIME, CONVERT(VARCHAR(10), GETDATE(), 120), 120))
        SET @end = DATEADD(day, 1, @start)


SELECT  CASE WHEN cd.DigitsDialed = '11783' THEN 'CCU01'
             WHEN cd.DigitsDialed = '11767' THEN 'CCU02'
             WHEN cd.DigitsDialed IN ( '11581', '11683' ) THEN 'CCU03'
             ELSE 'CCU04'
        END Line,
        SUM(CASE WHEN cd.CallDispositionFlag = 1
                      AND cd.PeripheralCallType IN ( 2, 4, 13 )
                      AND cd.CallDisposition IN ( 29, 13, 34, 28, 6 ) THEN 1
            END) Ans,
        SUM(CASE WHEN ( cd.CallDispositionFlag = 4
                        OR cd.CallDisposition IN ( 3, 1, 2 )
                      ) THEN 1
            END) Abn,
        SUM(CASE WHEN cd.CallDispositionFlag = 1
                      AND cd.PeripheralCallType IN ( 2, 4, 13 )
                      AND cd.CallDisposition IN ( 29, 13, 34, 28, 6 )
                      AND ( cd.NetQTime + cd.RingTime + cd.DelayTime ) <= 20
                 THEN 1
            END) Ans20,
        SUM(CASE WHEN cd.CallDisposition = 14 THEN 1
            END) Voicemail,
        SUM(CASE WHEN cd.CallDispositionFlag = 1
                      AND cd.PeripheralCallType IN ( 2, 4, 13 )
                      AND cd.CallDisposition IN ( 29, 13, 34, 28, 6 )
                 THEN cd.TalkTime
            END) TalkTime,
        SUM(cd.NetQTime) QTime,
        MAX(cd.TalkTime) MaxTalkTime,
        MAX(cd.NetQTime) MaxQTime
FROM    t_Termination_Call_Detail cd
WHERE   cd.DateTime >= @start
        AND cd.DateTime < @end
        AND cd.DigitsDialed IN ( '11581', '11683', '11722', '11767', '11783' )
GROUP BY CASE WHEN cd.DigitsDialed = '11783' THEN 'CCU01'
              WHEN cd.DigitsDialed = '11767' THEN 'CCU02'
              WHEN cd.DigitsDialed IN ( '11581', '11683' ) THEN 'CCU03'
              ELSE 'CCU04'
         END

The results give me the following format

                  Ans    Abn      Ans20   Voicemail      etc
CCU01        2
CCU02        2
CCU03        2
CCU04        3

I would now like to display the results as following, but I not sure how


                CCU01           CCU02        CCU03    CCU04
Ans
Abn
Ans20
Voicemail

etc

Is this possible?
Mark WilsonBI DeveloperAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

elimesikaCommented:
0
Máté FarkasDatabase Developer and AdministratorCommented:
You should rewrite your base query (remove groupping and summarize):
than place it in a pivot query:
SELECT  Field, [CCU01], [CCU02], [CCU03], [CCU04]
FROM (<base query>) B
PIVOT (SUM([Value]) FOR Field IN ([CCU01], [CCU02], [CCU03], [CCU04])) P

SELECT  CASE WHEN cd.DigitsDialed = '11783' THEN 'CCU01'
             WHEN cd.DigitsDialed = '11767' THEN 'CCU02'
             WHEN cd.DigitsDialed IN ( '11581', '11683' ) THEN 'CCU03'
             ELSE 'CCU04'
        END Line,
        CASE 
			WHEN cd.CallDispositionFlag = 1
                      AND cd.PeripheralCallType IN ( 2, 4, 13 )
                      AND cd.CallDisposition IN ( 29, 13, 34, 28, 6 ) THEN 'Ans'
			WHEN ( cd.CallDispositionFlag = 4
                        OR cd.CallDisposition IN ( 3, 1, 2 )
                      ) THEN 'Abn'
			WHEN cd.CallDispositionFlag = 1
                      AND cd.PeripheralCallType IN ( 2, 4, 13 )
                      AND cd.CallDisposition IN ( 29, 13, 34, 28, 6 )
                      AND ( cd.NetQTime + cd.RingTime + cd.DelayTime ) <= 20
                 THEN 'Ans20'
			WHEN cd.CallDisposition = 14 THEN 'Voicemail'
			END Field,
		1 [Value]
FROM    t_Termination_Call_Detail cd
WHERE   cd.DateTime >= @start
        AND cd.DateTime < @end
        AND cd.DigitsDialed IN ( '11581', '11683', '11722', '11767', '11783' )

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mark WilsonBI DeveloperAuthor Commented:
Thanks aqux3e

I have done as suggested and get the following error message

Invalid Column Name Field
0
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

Máté FarkasDatabase Developer and AdministratorCommented:
Sorry! Use "FOR Line IN" instead of Field in the pivot expression....
0
Mark WilsonBI DeveloperAuthor Commented:
Hi

I am now getting invalid Column Name Line

I have included the code as it is now
DECLARE @start DATETIME,
    @end DATETIME


        SET @start = DATEADD(day, -1, CONVERT(DATETIME, CONVERT(VARCHAR(10), GETDATE(), 120), 120))
        SET @end = DATEADD(day, 1, @start)
         
SELECT  Line, [CCU01], [CCU02], [CCU03], [CCU04]
FROM (SELECT  CASE WHEN cd.DigitsDialed = '11783' THEN 'CCU01' 
             WHEN cd.DigitsDialed = '11767' THEN 'CCU02' 
             WHEN cd.DigitsDialed IN ( '11581', '11683' ) THEN 'CCU03' 
             ELSE 'CCU04' 
        END Line, 
        CASE  
                        WHEN cd.CallDispositionFlag = 1 
                      AND cd.PeripheralCallType IN ( 2, 4, 13 ) 
                      AND cd.CallDisposition IN ( 29, 13, 34, 28, 6 ) THEN 'Ans' 
                        WHEN ( cd.CallDispositionFlag = 4 
                        OR cd.CallDisposition IN ( 3, 1, 2 ) 
                      ) THEN 'Abn' 
                        WHEN cd.CallDispositionFlag = 1 
                      AND cd.PeripheralCallType IN ( 2, 4, 13 ) 
                      AND cd.CallDisposition IN ( 29, 13, 34, 28, 6 ) 
                      AND ( cd.NetQTime + cd.RingTime + cd.DelayTime ) <= 20 
                 THEN 'Ans20' 
                        WHEN cd.CallDisposition = 14 THEN 'Voicemail' 
                        END Field, 
                1 [Value] 
FROM    t_Termination_Call_Detail cd 
WHERE   cd.DateTime >= @start 
        AND cd.DateTime < @end 
        AND cd.DigitsDialed IN ( '11581', '11683', '11722', '11767', '11783' )) B
PIVOT (SUM([Value]) FOR Line IN ([CCU01], [CCU02], [CCU03], [CCU04])) P 

Open in new window

0
Máté FarkasDatabase Developer and AdministratorCommented:
You sould use Field after SELECT  and Line in the PIVOT expression...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2005

From novice to tech pro — start learning today.