Skip to content
Advertisement

SQL: Get all values in column without using “case when”

Let’s say I have a table as shown above, and I want the percent of key observations that take the value A. I can do this using the following case when statements:

However, if I have lots of values for key then I would have to write lots of case when statements. Is there a simpler way to do this?

Advertisement

Answer

Try this below script. This will return percentage for all distinct keys.

If percentage is not required, then just do this as your sample code

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