It can be obtained in a single select???
An example company ID, department and production carried out
Obtain the percentage of production: the production of the person between the production of his department of his department ( production person * 100 / production department
Department production:
select departamento, sum(produción) from empresa group by departamento
worker production
select dni, departamento,sum(produción) from empresa group by dni,departamento
The query you are looking for is the following:
You can see how it works online at this link and it is divided into two parts.
The first part (the inner query) obtains the sum of each department and calls the result table
su
(the fieldsuma
would be accessed assu.suma
if there were a name conflict):The second part (the external query) relates each employee in the table
empresa
with the total results obtained by the previous query, so that each record obtained will have the total of the department associated in the fieldsuma
, being able to calculateproducción * 100 / suma
without problems in the fieldporc
.Searching for other plsql pages I have found another solution, but I have the doubt of which one is more optimal