Solved

Getting the percentage

Posted on 2014-12-23
7
85 Views
Last Modified: 2014-12-29
Hi,

I need your help please,

I have this query :

with 
t1 as (SELECT COUNT(AR.ApplicationID) AS [Provider and Network Management]
								FROM   AppRepository AR INNER JOIN ApplicationRepository_BusinessSubProcess ARBSP ON AR.ApplicationID = ARBSP.ApplicationID 
									   INNER JOIN BusinessSubProcess BSP ON ARBSP.SubProcessID = BSP.BusinessSubProcessId INNER JOIN
									   BusinessProcess BP ON BSP.BusinessProcessId = BP.BusinessProcessId
								WHERE  AR.Retired = 0  And AR.BusinessProcessId = 20 ),
t2 as (SELECT COUNT(AR.ApplicationID) AS [Product Management]
								FROM   AppRepository AR INNER JOIN ApplicationRepository_BusinessSubProcess ARBSP ON AR.ApplicationID = ARBSP.ApplicationID 
									   INNER JOIN BusinessSubProcess BSP ON ARBSP.SubProcessID = BSP.BusinessSubProcessId INNER JOIN
									   BusinessProcess BP ON BSP.BusinessProcessId = BP.BusinessProcessId
								WHERE  AR.Retired = 0  And AR.BusinessProcessId = 40 )
select [Provider and Network Management], [Product Management], [Provider and Network Management] + [Product Management] as Total
from t1, t2

Open in new window


The output for this query is this:
Provider and Network Management     Product Management            Total          
28                                                                       2                                          30


but what I want is this output

Provider and Network Management    PercentageOfProvider  Product Management     PercentageOfManagement       Total          
28                          93%                            2                     7%                    30



the PercentageOfProvider I got it from this formula

28*100/30 = 93%

2*100/30 = 7%  I rounded off

Thank you for your help!!!
0
Comment
Question by:lulu50
[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
  • 4
  • 2
7 Comments
 
LVL 18

Accepted Solution

by:
Simon earned 250 total points
ID: 40515882
Your code looks like MSSQL code, but I'm not entirely sure which RDBMS you're using as you've put the question in both MSSQL and MySQL topics.
This works for MSSQL. The only changes I've made are to the final SELECT statement.

--your cte definitions here
select [Provider and Network Management]
, round(100.0 * [Provider and Network Management] / ( [Provider and Network Management] + [Product Management] ),0) as PercentageOfProvider
, [Product Management]
, round(100.0 *[Product Management] /( [Provider and Network Management] + [Product Management]),0)  as PercentageOfManagement
, [Provider and Network Management] + [Product Management] as Total
from t1, t2

Open in new window


MSSQL2008 demo here: SQLFiddle

It is necessary to first multiply by a decimal value (100.0) to get a non-integer result of the division and then ROUND the result to zero decimal places.

If you need the "%" sign in the result column, you'd have to cast the result to int, then to varchar and then add +"%"
--your cte definitions here
select [Provider and Network Management]
, cast(cast(ROUND(100.0 * [Provider and Network Management] / ( [Provider and Network Management] + [Product Management] ),0) as int) as varchar(20)) + '%' as PercentageOfProvider
, [Product Management]
, cast(cast(ROUND(100.0 * [Product Management]/ ( [Provider and Network Management] + [Product Management] ),0) as int) as varchar(20)) + '%' as PercentageOfManagement
, [Provider and Network Management] + [Product Management] as Total
from t1, t2

Open in new window

0
 
LVL 56

Expert Comment

by:HainKurt
ID: 40515930
I posted this in your other question, but anyways...

with 
t1 as (select 28 [Provider and Network Management]),
t2 as (select 2 [Product Management])
select [Provider and Network Management], [Product Management], 
       [Provider and Network Management]+[Product Management] as Total,
	   100 * [Provider and Network Management] / ([Provider and Network Management]+[Product Management]) as [Percentage of Provider and Network Management],
	   100 * [Product Management] / ([Provider and Network Management]+[Product Management]) as [Percentage of Product Management]
from t1, t2

Provider and Network Management	Product Management	Total	Percentage of Provider and Network Management	Percentage of Product Management
28	2	30	93	6

Open in new window

0
 
LVL 56

Expert Comment

by:HainKurt
ID: 40515934
if you need "93%" here it is:

with 
t1 as (select 28 [Provider and Network Management]),
t2 as (select 2 [Product Management])
select [Provider and Network Management], [Product Management], 
       [Provider and Network Management]+[Product Management] as Total,
	   cast(100 * [Provider and Network Management] / ([Provider and Network Management]+[Product Management]) as varchar) + '%' as [Percentage of Provider and Network Management],
	   cast(100 * [Product Management] / ([Provider and Network Management]+[Product Management]) as varchar) + '%' as [Percentage of Product Management]
from t1, t2

Provider and Network Management	Product Management	Total	Percentage of Provider and Network Management	Percentage of Product Management
28	2	30	93%	6%

Open in new window

0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 56

Assisted Solution

by:HainKurt
HainKurt earned 250 total points
ID: 40515936
i guess we need some better fraction here

with 
t1 as (select 28 [Provider and Network Management]),
t2 as (select 2 [Product Management])
select [Provider and Network Management], [Product Management], 
       [Provider and Network Management]+[Product Management] as Total,
	   cast(cast(100.00 * [Provider and Network Management] / ([Provider and Network Management]+[Product Management]) as DECIMAL(10,2)) as varchar)+ '%' as [Percentage of Provider and Network Management],
	   cast(cast(100.00 * [Product Management] / ([Provider and Network Management]+[Product Management]) as DECIMAL(10,2)) as varchar) + '%' as [Percentage of Product Management]
from t1, t2

Provider and Network Management	Product Management	Total	Percentage of Provider and Network Management	Percentage of Product Management
28	2	30	93.33%	6.67%

Open in new window

0
 
LVL 56

Expert Comment

by:HainKurt
ID: 40515986
here (varchar in xx.xx% or just xx, rounded, integer):

with 
t1 as (select 28 [Provider and Network Management]),
t2 as (select 2 [Product Management])
select [Provider and Network Management], [Product Management], 
       [Provider and Network Management]+[Product Management] as Total,
	   cast(cast(100.00 * [Provider and Network Management] / ([Provider and Network Management]+[Product Management]) as DECIMAL(10,2)) as varchar)+ '%' as [Percentage of Provider and Network Management %],
	   cast(cast(100.00 * [Product Management] / ([Provider and Network Management]+[Product Management]) as DECIMAL(10,2)) as varchar) + '%' as [Percentage of Product Management %],
	   cast(round(100.00 * [Provider and Network Management] / ([Provider and Network Management]+[Product Management]), 0) as int)  as [Percentage of Provider and Network Management],
	   cast(round(100.00 * [Product Management] / ([Provider and Network Management]+[Product Management]), 0) as int) as [Percentage of Product Management]
from t1, t2

Provider and Network Management	Product Management	Total	Percentage of Provider and Network Management %	Percentage of Product Management %	Percentage of Provider and Network Management	Percentage of Product Management
28	2	30	93.33%	6.67%	93	7

Open in new window


use whatever you need...

some tips: use one word column alias
[Percentage of Product Management]  >>> Percent_Product_Management

use it short
Percent_Product_Management >>> pPrdMgmt

never convert data to some other types (unless absolutely necessary)
format them in your app

so instead of returning "2/3" to "66.67%" or "67%" or "67" just return 0.666666666 from query and format & use it in your application...
0
 
LVL 18

Expert Comment

by:Simon
ID: 40516192
@HainKurt: I'd already provided a working answer with a demo (although I'd shortened it to avoid using so many casts). Is there something else different in yours?
0
 

Author Closing Comment

by:lulu50
ID: 40521707
Thank you for all your help!!!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

630 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