Skip to content
Advertisement

Multiple sum subqueries for percentage

I need help with the following problem: I want to make a query that contains multiples sums and then takes those sums and uses them to get a percentage: percentage= s1/s1+s2. I have as input the following data: Orders shipping date, Nb of orders that have arrived late, Nb of orders that have arrived on time What I want as output: The percentage of orders that have arrived late and orders that have arrived on time. I want another column in the table that will have the percentage using SQL.

Concrete example:

In order to sum previous entries order numbers with status “LATE” to current on time order number I wrote the following sql: (sum1=s1)

The same kind of sql was written for “On Time” and it works. But what I need to do now is get the values and add them together of the two sql queries and based on the status which is late or on time do s1/s1+s2 or s2/s2+s1. My problem is that I do not know how to do this formula in a single query using those 2 subqueries, any help would be great.

Picture with Table

Above there is the link with the picture containing how the table looks(I am new so I am not allowed to embed a photo). The percentage column is the one I will add and there are lines pointing towards how that is calculated.

Advertisement

Answer

I created the table based on your image and added a few rows to it. In the query you could see total orders count per hour, per status and the grand total as you mentioned in the image.

The query looks like:

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