select case when ordertype=1 then gid else null end as gid,
case when ordertype=1 then client else null end as client,
id,barcode,name,size,unit,pice,number,total
from (
select gid,client,
cast(null as int) as id,cast(null as varchar(50)) as barcode,cast(null as varchar(50)) as name,cast(null as varchar(50)) as size,cast(null as varchar(50)) as unit,cast(null as numeric(10,2)) as pice,cast(null as numeric(10,2)) as number,cast(null as numeric(10,2)) as tota
,1 as ordertype
from aa
group by gid,client
union all
select gid,client,
id,barcode,name,size,unit,pice,number,total,
2 as ordertype
) as t
order by gid,client,ordertype,id
case when ordertype=1 then client else null end as client,
id,barcode,name,size,unit,pice,number,total
from (
select gid,client,
cast(null as int) as id,cast(null as varchar(50)) as barcode,cast(null as varchar(50)) as name,cast(null as varchar(50)) as size,cast(null as varchar(50)) as unit,cast(null as numeric(10,2)) as pice,cast(null as numeric(10,2)) as number,cast(null as numeric(10,2)) as tota
,1 as ordertype
from aa
group by gid,client
union all
select gid,client,
id,barcode,name,size,unit,pice,number,total,
2 as ordertype
) as t
order by gid,client,ordertype,id
(select top 100 percent * from
(
select top 100 percent id,barcode,name,size,unit,pice,number,total,gid,client from @a order by id
union all
select top 100 percent null,null,null,null,null,null,null,null,gid,client from @a group by gid,client
) a order by client,id
) b
你的语句有点出入,我的 G001 大地食品 是独立两列的