?
Solved

help with getting percentage value in sql column value

Posted on 2014-07-30
12
Medium Priority
?
259 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
[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
  • 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 35

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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 

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 35

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 35

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 1000 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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

752 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