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,类别
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,类别
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,类别
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,类别