How To Write a T-SQL Query that Computes and Displays the Percentage of Total Records Aggregated in Each Group of Records Generated by a GROUP BY Clause (256282)
The information in this article applies to:
- Microsoft SQL Server 6.5
- Microsoft SQL Server 7.0
This article was previously published under Q256282 SUMMARY
T-SQL does not contain a built in aggregate function that you can use directly in a grouping query to compute and display the percentage of the total number of rows aggregated in each group of records generated by the GROUP BY clause.
For example, in the SQL Server Pubs sample database it is possible to write a T-SQL GROUP BY query that displays the authors and the total number of titles written by each by using the COUNT aggregate function. However, you cannot calculate the percentage of the total number of titles written by each author by using any of the existing numeric aggregate T-SQL functions directly in the column list of the SELECT statement.
This article illustrates how to write a T-SQL GROUP BY query to compute and display the percentage of total records aggregated in each group by using a nested SELECT statement and the T-SQL CONVERT function.
Modification Type: | Minor | Last Reviewed: | 7/15/2004 |
---|
Keywords: | kbhowto KB256282 |
---|
|