----创建测试数据
declare @t table(出库单号 varchar(10),领料人 varchar(10),材料编号 varchar(10),数量 int,规格 varchar(10))
insert @t
select 'A','张三','a1',50,'xxx' union all
select 'A','张三','a2',21,'xx' union all
select 'B','李四','a2',45,'xx' union all
select 'B','李四','a3',56,'xx'
----生成排序临时表
select id = identity(int,1,1), * into #tmp from @t----查询
SELECT 出库单号 = case when not exists(select 1 from #tmp where 出库单号 = a.出库单号 and id < a.id) then 出库单号 else '' end,
总数量 = case when not exists(select 1 from #tmp where 出库单号 = a.出库单号 and id < a.id) then 
rtrim((select sum(数量) from #tmp where 出库单号 = a.出库单号 group by 出库单号)) else '' end,
领料人 = case when not exists(select 1 from #tmp where 出库单号 = a.出库单号 and id < a.id) then 领料人 else '' end,
材料编号,数量,规格
FROM #tmp a----删除临时表
drop table #tmp