Solved

help with getting percentage value in sql column value

Posted on 2014-07-30
12
241 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
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 extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

815 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

8 Experts available now in Live!

Get 1:1 Help Now