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
Solved

SQL Server crosstab - can i add a percentage column for each field?

Posted on 2009-05-04
6
536 Views
Last Modified: 2012-08-13
I am producing crosstabs in SQL and need to add a percentage column next to each of the field value columns. for example, in the code below, starting with column DM, the next column should be [PERCENT OF DM], where this should be the percent of the total of the DM field - this would add up to 100 for each column at the bottom of the xtab.

Is this possible? I've searched around on the web to no avail.
select MRC_YEAR,MRCRNG,[DM] ,[DM DMURL] ,[SPEC FIN],[NA] 
FROM 
(SELECT MRC_YEAR,MRCRNG,icontactid,CHANNEL,mrccode
FROM house where drops is null)    ps
PIVOT
(
Count (icontactid)
FOR CHANNEL IN
([DM] ,[DM DMURL] ,[SPEC FIN] ,[NA] )
)  AS pvt 
order by mrc_year,mrccode

Open in new window

0
Comment
Question by:ND_2007
  • 2
  • 2
  • 2
6 Comments
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 24298850
Is their a unique field in the cross tab results?  I had a similar situation about a month ago and I ended up just writing a second cross tab query w/ the percentages and then joining the results of both cross tabs together in a query.  Not sure if this will be posible in your scenario though.
0
 
LVL 1

Author Comment

by:ND_2007
ID: 24299035
Well, I could make a unique field based on two fields, but I expect to this for around 50 crosstabs, so hopefully i can get each crosstab in one query.  I might try this if all else fails though
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
ID: 24299810
you have to try like this
SELECT MRC_YEAR,MRCRNG,
       [DM] ,[DM]*100.0/(SELECT COUNT(*) FROM house WHERE drops IS NULL AND CHANNEL = 'DM') as Percentage_DM,
       [DM DMURL] ,[DM DMURL]*100.0/(SELECT COUNT(*) FROM house WHERE drops IS NULL AND CHANNEL = 'DM DMURL') as [Percentage_DM DMURL],
       [SPEC FIN],[SPEC FIN]*100.0/(SELECT COUNT(*) FROM house WHERE drops IS NULL AND CHANNEL = 'SPEC FIN') as [Percentage_SPEC FIN],
       [NA] 
 FROM (SELECT MRC_YEAR,MRCRNG,icontactid,CHANNEL,mrccode
         FROM house where drops is null)    ps
        PIVOT (Count (icontactid) FOR CHANNEL IN ([DM] ,[DM DMURL] ,[SPEC FIN] ,[NA] ))  AS pvt 
order by mrc_year,mrccode

Open in new window

0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Closing Comment

by:ND_2007
ID: 31577703
That's the ticket......thanks!!
0
 
LVL 14

Expert Comment

by:Christopher Gordon
ID: 24304562
Nice Job Sharath_123, I'll be trying that one out in the future too :)
0
 
LVL 40

Expert Comment

by:Sharath
ID: 24305389
thanks, happy to help you.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 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