答案 0 :(得分:1)
以下是一种使用union all
和聚合的方法:
select weeknumber, sum(opened) as opened, sum(closed) as closed
from ((select OpenWeekNumber as weeknumber, 1 as Opened, 0 as Closed
from maintable
) union all
(select ClosedWeekNumber as weeknumber, 0 as Opened, 1 as Closed
from maintable
where ClosedWeekNumber > 0
)
) t
group by weeknumber
order by weeknumber;