Solved

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

Posted on 2009-05-04
6
531 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
Need to grow your business through quality cloud solutions? With everything required to build a cloud platform and solution, you may feel like the distance between you and the cloud is quite long. Help is here. Spend some time learning about the Con…

930 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

13 Experts available now in Live!

Get 1:1 Help Now