#Try this, assuming it's a MySQL table:
$sql = "SELECT DISTINCT GID FROM table";
$result = mysql_query($sql,$db);
while ($row = mysql_fetch_array($result))
{
$emails = array();
$gid = $row[GID];
$sql = "SELECT EMAIL FROM table WHERE GID='$gid'";
$result2 = mysql_query($sql,$db);
while ($row2 = mysql_fetch_array($result2))
{
$emails[] = $row2[EMAIL];
}
$emails = implode(',', $emails);
$sql = "INSERT INTO table2 (GID,EMAIL) VALUES ('$gid', '$emails')"
mysql_query($sql,$db);
}