A Pivot question

How can I use the Pivot table to change the following data layout

Type  Text  Value
----------------------
Com    abcd     1
Com    efgh      2
Net     wxyz     3
Net     abcd     1

into this layout?

Text   Com   Net
------------------------
abcd     1       1
efgh      2       0
wxyz     0      3

Thanks.
LVL 1
minglelinchAsked:
Who is Participating?
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.

coreconceptsCommented:
The best way to use pivot is with a CTE (as opposed to subquery) -- it's just easier:

Let's say that your initial query was this:

SELECT 
   Type
, Text
, Value
FROM YourTable

Open in new window


-- even if there's a lot of joining it doesn't matter...

What you will want to do is make this a CTE
With MyPivotTable As
(
SELECT 
   Type
   , Text
   , Value
FROM YourTable
)
             SELECT
                Text
                 , [com]
                 , [net]
              FROM MyPivotTable
                        PIVOT(MAX(Value) FOR TYPE IN ([Com], [net])) As P

Open in new window


Here's the only gotchya, you have to already know which values you want as your "spreading values" your spreading column is the TYPE column.  But you had to hard code in [com] and [net] in the example above.  

Replace the Query in the top half of the CTE with your actual query and run this it should work.  Also note that I used an aggregate in the PIVOT statement :  "Max(Value)" you can change that to just Value if you prefer.
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
minglelinchAuthor Commented:
Nice answer. Thank you.
0
PortletPaulfreelancerCommented:
:) it is a very nice answer.

My antique view though is that a good old fashioned way may still be easier.
select
          Text
        , isnull(max(case when type = 'Com' then value end),0) as Com
        , isnull(max(case when type = 'Net' then value end),0) as Net
from your_table
group by
          Text

Open in new window

but I guess that's because I'm used to it. We used to call this pivoting before the 'pivot feature' arrived on the scene.
0
minglelinchAuthor Commented:
Excellent!  Thanks for the simple solution!
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
SSRS

From novice to tech pro — start learning today.