Skip to content
Advertisement

Subtract from rows with certain values

I have a table created in Materialized view from 10 different tables.

Part of it looks like this

group_name value1 value2
group1 100 20
group2 200 40
unknown 300 60
TOTAL 600 120

I have to rearrange all values from rows with value group_name = ‘unknown’ to other rows. The final table should look like this

group_name value1 value2
group1 200 40
group2 400 80
TOTAL 600 120

So formula for ‘group1’ would be:

The table is created with massive code and please note – I didn’t write it, it was given to me and I have to work with it. I don’t like how it looks, so please spare your anger. Anyway, the query looks like this:

I have no idea how should I write such conditions in SQL. Please help.

Advertisement

Answer

Distribute ‘unknown’ row to other rows. Assuming value1, value2 are DECIMAL

db<>fiddle

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