Skip to content
Advertisement

Combining data from one column into one with multiplication

Results returned by the query

I am trying to find a way to add up amount with the same ID and different units but also perform multiplication or division on them before adding them together. The column time describes the amount of time spent doing a certain task. There are four different values the time column can have which are: – Uur (which stands for Hours) – Minuut (which stands for Minutes) – Etmaal (which stands for 24 hours) – Dagdeel (which stands for 4 hours)

What I’d like is to transform them all into hours which should eventually return the row:

ID | Amount  | Time |
---------------------
82 | 1690634 | Uur  |

So only one unit remains.

This means rows that contain minuut will have their amount divided by 60 Rows that contain etmaal will have their amount multiplied by 24 and rows that contain dagdeel will have their amount multiplied by 4

Advertisement

Answer

I think the logic is:

select id, 
       (case when time = 'minuut' then amount / 60
             when time = 'etmaal' then amount * 24
             when time = 'dagdeel' then amount / 4
             when time = 'uur' then amount
        end) as amount,
       time
from table
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement