Skip to content
Advertisement

How to make multiple aggregate function without break / divided the fields

Is it possible to make an aggregate function without break / divided the grouping fields? I make a query but it will divided into duplicate value in the first field, here is my query:

My expected result:

Emp. Name Count of Prod Total Account % Prod Duration Trip
Emp.1 62 63 98,41% 30
Emp.2 45 48 93,75% 28
Emp.3 20 22 90,91% 25
Emp.4 20 24 83,33% 22
Emp.5 15 19 78,95% 20

Thank you in advance.

Advertisement

Answer

If you want one row per empname, then that should be the only column in the group by (or perhaps other columns that describe each employee without multiplying the number of rows).

That suggests something like this:

Note some of the changes to the query:

  • The column aliases are simplified so no escape characters are needed.
  • The GROUP BY has only empname.
  • The logic for the proportion of products is simplified using AVG().
  • I assume that the trip duration should be a sum of the individual durations.
  • I did not remove it, but empname IS NOT NULL is redundant, because the NOT IN handles this.
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement