[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2009-05-04
6
Medium Priority
?
544 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 2000 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 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