That is not VBA code, it is SQL.

Should provide sample data and desired output. Can build tables in post or attach file.