Solved

help with getting percentage value in sql column value

Posted on 2014-07-30
12
251 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 34

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

 

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 34

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 34

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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.
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, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

691 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