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

解决方案 »

  1.   

    TO rea1gz(冒牌realgz V0.3)语句好像有点问题,请不通的?再说有没有更简洁一点的方法呢?
      

  2.   

    你的这样的报表本来就不应该交给服务器做,sql server不擅长报表格式的变换
      

  3.   

    这个用SQL语句整理好,方便些吧,麻烦再帮看看
      

  4.   

    select case when id is null then client else ltrim(id) end id,case when barcode is null then ltrim(gid) else barcode end barcode,name,size,unit, pice, number,total from 
    (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
      

  5.   

    TO chuifengde
    你的语句有点出入,我的 G001   大地食品 是独立两列的