paginate with sqlserver

I do a sum query, the sql looks like below:

select material.code as material_code,material.name as material_name,        sum(inventories.qty) as qty from inventories      inner join materials on inventories.material_id=materials.id group by material.code,material.name

It run correctly.But when I add the group item, one day the rails told me something error.And report the identifier is too long.Then I find,each time,rails alway run a sql like below:

select count(*) as count_all,material.code,material.name as material_code_material_name from inventories      inner join materials on inventories.material_id=materials.id group by material.code,material.name

when I add the group item,one day the identifier like material_code_material_name become too long

why?? And How can I fix it??