SQLTeam.com | Weblogs | Forums

Calculate percentage in SQL

sql2008r2

#1

Hi,

I have view called View_Status.
Column are StatusNo, Status, Status Count.

the data is given below

StatusNo   Status             StatusCount
1               VALID             121
2               NOT DONE      48
3               NO SHOW       20
4               EXPIRED        8

I want to calculate % of each status

below is the result.

StatusNo   Status             StatusCount        Percentage
1               VALID             121                     62%
2               NOT DONE      48                      24%
3               NO SHOW       20                      10%
4               EXPIRED        8                         4%

How to do the query

Thanks
Basit


#2

i got the answer with below code

SELECT [Status]
     , [StatusCount]
     , ([StatusCount] * 100) / (SELECT SUM([StatusCount]) FROM [Status]) AS Total_Percentage
FROM [Status]