Skip to content
Advertisement

How can I calculate percentages from total rows and status in sql?

I have a table that contains a list of templates and statuses that I am trying to calculate percentages on.

My schema looks like the following:

SQL SQL to Create DB

Sample Data:

What I’m trying to achieve:

The query I have so far is the following:

The problem I have is that my percentages return nothing atm, you can see my sqlfiddle here.

Additionally as you can see I have inner queries for each status, as the number of statuses grow I suspect the query will be hard to read/maintain, is there a more efficient way to do this?

Advertisement

Answer

You can use conditional aggregation:

In other databases, you would need to use more standard syntax:

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