select dept,'1104-2000' as 类别,Sum(数量) as 数量
into #tempTable
from table
where substring(sa_product,1,4) between 1104 and 2000   (条件是你写的,没仔细看)
group by dept
insert into #temptabel(dept,类别,数量)
select dept,'1104-2000' as 类别,Sum(数量)
from table
where not (substring(sa_product,1,4) between 1104 and 2000 )select * from #temptable order by dept,类别

解决方案 »

  1.   

    select sm_dept,deptname,case when 类别='1101' then sum(num) else 0 end,
    case when 类别='1102' then sum(num) else 0 end,
    case when 类别='1103' then sum(num) else 0 end,
    case when 类别='other' then sum(num) else 0 end
    from #temptablegroup by sm_dept,deptname,类别
      

  2.   

    case  when (substring(sa_product,1,4) in(1101,1102,1103)) then substring(sa_product,1,4) 
    when (substring(sa_product,1,4) between 1104 and 2000) then '1104-2000' 
    else 'others' end as 类别, 
    sum(saleamt) as 数量
    -----group by by sm_dept,类别
      

  3.   

    因为你from的表很多,我只是把主要的写出来,你改改试试