Solved

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

Posted on 2009-05-04
6
537 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
[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
  • 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 41

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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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 41

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.

733 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