Skip to content
Advertisement

Divide two values in aggregation functions?

I have a table view like this:

Now for each week I need to do below things:

  • For each week, how many total PART and TEMP are there. Let’s call this total. In ColF I can have other values as well apart from PART and TEMP like empty string or any other values. I just need to count PART and TEMP only.
  • For each week, how many TEMP are there. Let’s call this temp.
  • For each week, divide temp/total and get the result by 3 decimal places. If the result is coming as 0 then it should show only 0.

So output should be like this. Basically group by on week column.

So I tried like this and it does the job but do I need to calculate the percentage first and then divide by 100 to get what I need? I do think this can be improved so that I should not calculate percentage first. I should be able to get the result as it is without doing percentage.

Is this possible to do by any chance?

Advertisement

Answer

You can simplify the ratio calculation by just using 1.0:

Or, more simply:

Here is a db<>fiddle (using Postgres).

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