Skip to content
Advertisement

SQL Server: Counts and Percentage by Group

I’ve tried the suggestions I’ve found on SO and cannot get the results I’m looking for which is this:

I am able to get the first two columns with this code:

— What I’ve tried:

Trying to use a calcuation in a subquery to represent the total number of rows in the table is beyond me so far, so I thought I’d just use the actual number. But all I get is zeros in the result.

These are some other things I’ve tried based on the posts I found here on SO:

The query below returns all zeroes, too:

This query follows a similar example I found on SO but it does not give the results I need:

Advertisement

Answer

You can use a window function and arithmetic:

This returns a ratio between 0 and 1. If you want a value between 0 and 100 multiply by 100.0 instead.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement