I think u need to get data in code like u mention above may be for report or chart , better declare data-table and use query to retrieve one by one and bind value dynamically in that data-table by programming code.
use this query to take id only
select id from tablename group by id
, then loop with result,
e:g id '1 and 2 u will get 2 result each,
so first value bind in col 1 and 2 value bind in col 2 , same like for id - 2
first value bind in col 1 and 2 value bind in col 2