Use coalesce() and subquery
select id, o1, CASE WHEN o2!=o1 THEN o2 END o2, CASE WHEN o3!=o2 THEN o3 END o3 FROM ( select id, coalesce(org1,org2,org3) o1, coalesce(org2,org3) o2, org3 o3 from tbl ) t
UPDATE
The previous answer was insufficient, because R2D2 turned out to be completely correct. Unfortunately, you cannot do CTE in mysql, so I created a view instead (I extended the example with another org4 column):
CREATE VIEW vert AS select id i,1 n, org1 org FROM tbl where org1>'' UNION ALL select id,2, org2 FROM tbl where org2>'' UNION ALL select id,3, org3 FROM tbl where org3>'' UNION ALL select id,4, org4 FROM tbl where org4>'';
In this view, you can now do the following:
SELECT id, (select org from vert where i=id order by n limit 1) org1, (select org from vert where i=id order by n limit 1,1) org2, (select org from vert where i=id order by n limit 2,1) org3, (select org from vert where i=id order by n limit 3,1) org4 FROM tbl
Not pretty, but it does its job, see here: SQLfiddle
input:
| id | org1 | org2 | org3 | org4 | |----|--------|--------|---------|--------| | 1 | HR | (null) | Staff | IT | | 2 | (null) | IT | Dev | (null) | | 3 | (null) | (null) | Finance | HR |
output:
| id | org1 | org2 | org3 | org4 | |----|---------|-------|--------|--------| | 1 | HR | Staff | IT | (null) | | 2 | IT | Dev | (null) | (null) | | 3 | Finance | HR | (null) | (null) |