Solved

Getting the percentage

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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