Solved

Getting the percentage

Posted on 2014-12-23
7
80 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
  • 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 51

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 51

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 51

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 51

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

830 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