Try this. You may need to modify the SQL syntax to get it working with DB2 but teh concept will remain the same. i.e. Use the following query on TableA to return a temporary table Temp.
SELECT DISTINCT ID, ID2 FROM TableA where Flag1 = 'Y'
Then the final query will Join tableB with the temp table derived from TableA instead of TableA. So, the final query will be:
Select Temp.ID as Col1, TableB.ID as Col2, TableB.Name From
(SELECT DISTINCT ID, ID2 FROM TableA where Flag1 = 'Y') Temp
LEFT JOIN TableB On Temp.ID2 = TableB.ID
Hope this helps.