Solved

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

Posted on 2009-05-04
6
529 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 1

Author Closing Comment

by:ND_2007
Comment Utility
That's the ticket......thanks!!
0
 
LVL 14

Expert Comment

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

Expert Comment

by:Sharath
Comment Utility
thanks, happy to help you.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

771 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now