I am trying to come up with a solution without a subquery which was asked in one of the interview.
customerId
in Orders table is a foreign key referencing id
in Customer table.
Table: Customer: id
is a primary key
Table Orders
I was asked to calculate a percentage of each customer orders as compared to total units across the orders table. So, output should look like below:
Output:
Below was my solution:
select
c.name,
COALESCE(SUM(o.units),0)/(select SUM(units) from orders)*100 as units
FROM customer c LEFT OUTER JOIN orders o ON c.id = o.customerid
group by c.id
Then interviewer asked to come up with a single query solution without using a subquery.
My answer was since we need a grouped sum based on customer id and then total sum of units, we will need two queries.
Does anyone know how this can be solved without using a subquery?
Note:
I see couple of comments saying it will error out/ query is not valid, however, it runs fine:
2
Answers
You can achieve what you want by using a windowed aggregate function to replace the subselect.
Typically, the overall total would be calculated with something like
SUM(o.units) OVER()
. However, since we are already within the scope of a group-by, we need to reference the group-by sum within the window function sum –SUM(SUM(o.units)) OVER()
.The
OVER()
qualifier without aPARTITION BY
orORDER BY
sets the window scope to be the entire result set (the grand total).Results:
See this db<>fiddle for a demo.