select 订单号,sum(case when 月份=1 then 数量 else 0 end) as [1月], sum(case when 月份=2 then 数量 else 0 end) as [2月], sum(case when 月份=3 then 数量 else 0 end) as [3月], sum(case when 月份=4 then 数量 else 0 end) as [4月] ...... from tb group by 订单号
SELECT * FROM #tb pivot(sum([数量]) for [月份] in ([1],[2],[3],[4],[5],[6],[7],[8],[9],[10],[11],[12])) as pvt
from tb
group by 订单号, 月份
select 订单号,sum(case when 月份=1 then 数量 else 0 end) as [1月],
sum(case when 月份=2 then 数量 else 0 end) as [2月],
sum(case when 月份=3 then 数量 else 0 end) as [3月],
sum(case when 月份=4 then 数量 else 0 end) as [4月]
......
from tb group by 订单号