Solved

Getting the percentage

Posted on 2014-12-23
7
73 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:
SimonAdept earned 250 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 51

Assisted Solution

by:HainKurt
HainKurt earned 250 total points
Comment Utility
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
Comment Utility
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:SimonAdept
Comment Utility
@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
Comment Utility
Thank you for all your help!!!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Sql server lock cursor 13 39
Distinct values from two tables 14 17
Sql query 34 16
Delete Trigger in SQL Server2008R2 5 0
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
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…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

12 Experts available now in Live!

Get 1:1 Help Now