?
Solved

SQL Server Pivot

Posted on 2013-01-29
2
Medium Priority
?
284 Views
Last Modified: 2013-01-30
I have a database table where the data looks like this:

CadenceDetailID      FKCadenceID      Priority      Occurence      Multiplier
1                                       1                               1                 1                            1
2                                       1                                1                 2                               0
3                                       1                                2                 1                               0
4                                        1                               2                  2                              1

I need the output to look like this:

Wall      1      2
1      1      0
2      0      1

Wall = Occurence
1 = Multiplier Where Priority = 1
2 = Multiplier Where Priority = 2

I am getting this result back:

Wall      1      2
1      0      2
1      1      NULL
2      0      NULL
2      1      2


This is my query:

select Occurence AS Wall,Multiplier, [1], [2]
from (
      select Occurence, Multiplier,Priority, row_number() over (partition by Occurence order by Occurence) rn
      from tblCadenceStrategydetails_test
) o
pivot(max(Priority) for rn in ([1], [2])) p
ORDER BY Occurence

Could someone please help me with this.  I would be truly appreciative.

Thank you.
0
Comment
Question by:sherbug1015
[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 25

Accepted Solution

by:
chaau earned 2000 total points
ID: 38833438
This is what you are after:

select Occurence AS Wall, [1], [2]
from (
      select Occurence, Multiplier, Priority
      from tblCadenceStrategydetails_test
) o
pivot(MAX(Multiplier) for Priority in ([1], [2])) p
ORDER BY Occurence

Open in new window


SQL Fiddle
0
 

Author Closing Comment

by:sherbug1015
ID: 38835147
Thank you
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

752 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