Вы можете использовать:
select week_no, sum(nvl(Inflow,0)) as Inflow,
sum(nvl(Outflow,0)) as Outflow,
sum(nvl(Total_Backlog,0)) as Total_Backlog
from
(
select to_char(Start_Date,'IW') Week_No,
count(CASE_ID) as Inflow,
( case when STATUS != 'Close' then count(CASE_ID) end ) as Total_Backlog,
null Outflow
from my_table
group by to_char(Start_Date,'IW'), status
union all
select to_char(End_Date,'IW') Week_No,
null as Inflow, null as Total_Backlog,
( case when STATUS = 'Close' then count(CASE_ID) end ) as Outflow
from my_table
where End_Date is not null
group by to_char(End_Date,'IW'), status
)
group by week_no
order by week_no;
WEEK_NO INFLOW OUTFLOW TOTAL_BACKLOG
40 7 3 3
41 2 2 1
Rextester Demo
или вы можете предпочесть немного другоепуть как:
select week_no, sum(nvl(Inflow,0)) as Inflow,
sum(nvl(Outflow,0)) as Outflow,
sum(nvl(Total_Backlog,0)) as Total_Backlog
from
(
select to_char(Start_Date,'IW') Week_No,
count(CASE_ID) as Inflow,
count( case when STATUS != 'Close' then CASE_ID end ) as Total_Backlog,
null Outflow
from my_table
group by to_char(Start_Date,'IW')
union all
select to_char(End_Date,'IW') Week_No,
null as Inflow, null as Total_Backlog,
count( case when STATUS = 'Close' then CASE_ID end ) as Outflow
from my_table
where End_Date is not null
group by to_char(End_Date,'IW')
)
group by week_no
order by week_no;
WEEK_NO INFLOW OUTFLOW TOTAL_BACKLOG
40 7 3 3
41 2 2 1