Given the following table:
... | id | type | ...
... | 1 | 1 | ...
... | 1 | 2 | ...
... | 1 | 3 | ...
... | 1 | 4 | ...
... | 2 | 1 | ...
... | 2 | 2 | ...
... | 3 | 1 | ...
... | 3 | 3 | ...
... | 4 | 1 | ...
... | 5 | 4 | ...
I want to write a select query that will return rows
and change the return value to from 4 to 3 if (the same id and type 3 & 4) existed.
Noted that if only type = ‘4’ and without the (same id & type = ‘3’) existed,
the result will not be modified.
Example query that doesn’t work as it can’t check the upper conditions:
SELECT id,
CASE WHEN (type = ‘4’)
THEN ‘3’
ELSE type end AS type
FROM table;
My desired output would be:
... | id | type | ...
... | 1 | 1 | ...
... | 1 | 2 | ...
... | 1 | 3 | ...
... | 1 | 3* | ...
... | 2 | 1 | ...
... | 2 | 2 | ...
... | 3 | 1 | ...
... | 3 | 3 | ...
... | 4 | 1 | ...
... | 5 | 4 | ...
2
Answers
To update:
To select:
I have one, this is to many step but at least is work. You can simplefy it by your self.