Solved

help with getting percentage value in sql column value

Posted on 2014-07-30
12
243 Views
Last Modified: 2014-08-06
I have the below query and value and I appreciate if I can get a percentage value I decimals with a percentage symbol also:

select b.Desc, (count(a.Status)*100 / (Select COUNT (*) from dbo.Test_T)) As Value, COUNT(a.Status)

From dbo.Test_T a, dbo.Status_T b

where a.Status = b.Status

group by b.Desc

currently I got values like:

Desc      Value      (No column name)
Closed      94      362
Open      5      23

I need:

Desc      Value      (No column name)
Closed      94.0%      362
Open      5.0%      23
0
Comment
Question by:welcome 123
  • 6
  • 3
  • 3
12 Comments
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40229286
select b.Desc, str(((100.0*count(a.Status)) / sum((Select COUNT (*) from dbo.Test_T)) over ()), 5, 1)+'%' As Value, COUNT(a.Status)

Open in new window

0
 
LVL 33

Expert Comment

by:ste5an
ID: 40229315
Format it in the front-end..
0
 

Author Comment

by:welcome 123
ID: 40229333
I got the result as:

Desc      Value      (No column name)
Closed       47.0%      362
Open        3.0%      23

can I get help with getting the count of open and closed etc
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:welcome 123
ID: 40229334
there is no front end
0
 
LVL 21

Expert Comment

by:Randy Poole
ID: 40229385
Normally you would post an additional question for that portion
0
 

Author Comment

by:welcome 123
ID: 40229411
I got the result wrong with your answer:

Desc      Value      (No column name)
 Closed       47.0%      362
 Open        3.0%      23


The percentage is not 47% for total of 385 records whats the percentage of 362 ? like 94.02% that is what I want
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40229414
You let work your users with SSMS..?
0
 

Author Comment

by:welcome 123
ID: 40229432
this is for a one time report but lost of data so I work on ssms one time. Can someone help me with the answer I am looking for instead of asking me questions not related please
0
 

Author Comment

by:welcome 123
ID: 40229434
I means lots of data there is a typos
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40229442
Sorry, it's only a materialzed result set in SSMS. A report would be pasting that to Excel.. which is really code at formatting values.
0
 

Author Comment

by:welcome 123
ID: 40229476
the query:

select b.Desc, str(((100.0*count(a.Status)) / sum((Select COUNT (*) from dbo.Test_T)) over ()), 5, 1)+'%' As Value, COUNT(a.Status)

Randy posted does give me the right answer here :

I have total rows of 385 and out of which 362 are closed status so I need a percentage for that which I could get using my query initially but an getting a rounded value instead in need to get the 2 decimals also that is what I am asking:

this is my query:

select b.Desc, (count(a.Status)*100 / (Select COUNT (*) from dbo.Test_T)) As Value, COUNT(a.Status)

 From dbo.Test_T a, dbo.Status_T b

 where a.Status = b.Status

 group by b.Desc
0
 
LVL 21

Accepted Solution

by:
Randy Poole earned 250 total points
ID: 40229636
select b.Desc, str(((100.0*count(a.Status)) / (Select COUNT (*) from dbo.Test_T)), 5, 1)+'%' As Value, COUNT(a.Status)

Open in new window

0

Featured Post

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.

Question has a verified solution.

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

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

839 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