I have a query that returns the following table:
Year IsFunded NotFunded 2003 Null 4 2003 3 Null 2004 Null 2 2004 8 Null
So I need:
SELECT Year, IsFunded, NotFunded FROM ( --myQuery that returns unflattened results )
I just need one row for each year. like:
Year IsFunded NotFunded 2003 3 4 2004 8 2
Use GROUP BY
and MAX
:
SELECT t.Year, MAX(t.IsFunded) AS IsFunded, MAX(t.NotFunded) AS NotFunded
FROM
(
--myQuery that returns unflattened results
) AS t
GROUP BY t.Year;
I would replace null with 0 while sum and just add group by year, like below
with MyQuery(Year,IsFunded,NotFunded) as (
select 2003,Null,4 from dual union
select 2003,3,Null from dual union
select 2004,Null,2 from dual union
select 2004,8,Null from dual
)
select year,sum(case when IsFunded is null then 0 else IsFunded end) as IsFunded,
sum(case when NotFunded is null then 0 else NotFunded end) as NotFunded
from MyQuery group by year
Output
YEAR ISFUNDED NOTFUNDED
2003 3 4
2004 8 2
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.