I have to transpose my rows into columns from a DB2 table.This is how my table is structured..
ItemID Item Value
---------------------
1 Meeting
As @bhamby said, DB2 doesn't have a PIVOT
function.
Mostly, my query just differs in how the results are retrieved - you'd need to run the profiler/optimizer over them to be sure, but I believe that the correlated sub-queries may be executed per-row (potentially less efficient), rather than as sets. This is unlikely to be an issue over small datasets.
WITH Item (id) as (SELECT DISTINCT itemId
FROM YourTable),
SELECT item.id, Meeting.meeting, Advise.advise, NoAdvise.noadvise
FROM Item
LEFT JOIN (SELECT itemId, value as meeting
FROM YourTable
WHERE item = 'Meeting') as Meeting
ON Meeting.itemId = Item.id
LEFT JOIN (SELECT itemId, value as advise
FROM YourTable
WHERE item = 'Advise') as Advise
ON Advise.itemId = Item.id
LEFT JOIN (SELECT itemId, value as noadvise
FROM YourTable
WHERE item = 'NoAdvise') as NoAdvise
ON NoAdvise.itemId = Item.id
(... Actually, I'm a little concerned that you have columns for both 'advise' and 'no advise', which would appear to be some sort of boolean condition - ie, you should have one, but not the other).