Solved

Getting the percentage

Posted on 2014-12-23
7
84 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 53

Expert Comment

by:Huseyin KAHRAMAN
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 53

Expert Comment

by:Huseyin KAHRAMAN
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
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 53

Assisted Solution

by:Huseyin KAHRAMAN
Huseyin KAHRAMAN 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 53

Expert Comment

by:Huseyin KAHRAMAN
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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