如何从SQL查询中展平结果?

时间:2015-09-13 13:33:11

标签: sql oracle

我有一个返回下表的查询:

Year    IsFunded     NotFunded
2003    Null         4
2003    3            Null
2004    Null         2
2004    8            Null

所以我需要:

SELECT Year, IsFunded, NotFunded
FROM
(
    --myQuery that returns unflattened results
)

我每年只需要一排。 像:

Year    IsFunded     NotFunded
2003    3            4
2004    8            2

1 个答案:

答案 0 :(得分:4)

使用GROUP BYMAX

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;