2016-03-07 55 views

回答

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; 
相關問題