我不懂得SQL SERVER语法,不过:
select c.company company,
c.customer customer,
SUM(cur_year_ytd) cur_year_ytd,
SUM(prev_year_ytd) prev_year_ytd
into newtable
from
(
select company, customer,
CASE year
WHEN @cur_yr THEN
case @numMon
when 1 then m1
when 2 then m1 + m2
...
when 12 then m1 + m2 + .... + m12
end
ELSE 0
END as cur_year_ytd
,WHEN @pre_yr THEN
case @numMon
when 1 then m1
when 2 then m1 + m2
...
when 12 then m1 + m2 + .... + m12
end
ELSE 0
END as prev_year_ytd
from oldtable
where year = @cur_yr OR year = @pre_yr
) c
GROUP BY c.company,c.customer
HAVING SUM(cur_year_ytd) > 0.00 or SUM(prev_year_ytd) > 0.00
select c.company company,
c.customer customer,
SUM(cur_year_ytd) cur_year_ytd,
SUM(prev_year_ytd) prev_year_ytd
into newtable
from
(
select company, customer,
CASE year
WHEN @cur_yr THEN
case @numMon
when 1 then m1
when 2 then m1 + m2
...
when 12 then m1 + m2 + .... + m12
end
ELSE 0
END as cur_year_ytd
,WHEN @pre_yr THEN
case @numMon
when 1 then m1
when 2 then m1 + m2
...
when 12 then m1 + m2 + .... + m12
end
ELSE 0
END as prev_year_ytd
from oldtable
where year = @cur_yr OR year = @pre_yr
) c
GROUP BY c.company,c.customer
HAVING SUM(cur_year_ytd) > 0.00 or SUM(prev_year_ytd) > 0.00