Skip to content
Advertisement

How to sum few colums values for each type and calculate percentage?

I have below query which gives me data with three columns – type, amount and total for current week using week_number column.

Below is the output I get back as of now with above query:

Problem Statement

Now I need to calculate the percentage for each type by using below formula:

  • For each type (sum(total) – sum(amount)) / sum(amount).
  • So for PROC1 type, (sum of all ‘total’ coumn – sum of all ‘amount’ column) / sum of all ‘amount’ column.
  • Similarly for PROC2 and PROC3 as well.
  • Also I need extra OVERALL type as well which will be sum of all PROC1 + PROC2 + PROC3 values.

At the end I should be seeing this output:

Is this possible to do in redshift?

Advertisement

Answer

You can phrase this as:

In practice, this will put the “Overall” column last. However, that is not guaranteed unless you add an ORDER BY.

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