Solved

help with getting percentage value in sql column value

Posted on 2014-07-30
12
238 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
Comment Utility
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 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
Format it in the front-end..
0
 

Author Comment

by:welcome 123
Comment Utility
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
 

Author Comment

by:welcome 123
Comment Utility
there is no front end
0
 
LVL 21

Expert Comment

by:Randy Poole
Comment Utility
Normally you would post an additional question for that portion
0
 

Author Comment

by:welcome 123
Comment Utility
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
Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
You let work your users with SSMS..?
0
 

Author Comment

by:welcome 123
Comment Utility
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
Comment Utility
I means lots of data there is a typos
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

728 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

10 Experts available now in Live!

Get 1:1 Help Now