总结Oracle中的空行

use*_*653 4 sql oracle oracle11g

我有这样的数据集:

+---------------+-------+
| SAMPLE_NUMBER | SCORE |
+---------------+-------+
|             1 | 100   |
|             2 | 97    |
|             3 | 124   |
|             4 | 762   |
|             5 | 999   |
|             6 | 1200  |
|             7 | NULL  |
|             8 | NULL  |
|             9 | NULL  |
|            10 | NULL  |
+---------------+-------+
Run Code Online (Sandbox Code Playgroud)

我希望能够汇总NULL行而不是全部显示它们.理想情况下,我希望上面看起来像这样:

+---------------+-------+
| SAMPLE_NUMBER | SCORE |
+---------------+-------+
| 1             | 100   |
| 2             | 97    |
| 3             | 124   |
| 4             | 762   |
| 5             | 999   |
| 6             | 1200  |
| 7-10          | NULL  |
+---------------+-------+
Run Code Online (Sandbox Code Playgroud)

Oracle有什么方法可以做到这一点吗?或者是我将不得不做后检查?

Gor*_*off 7

是.对于您的样本数据:

select (case when score is null then min(sample_number) || '-' || max(sample_number)
             else min(sample_number)
        end) as sample_number,
       score
from table t
group by score
order by min(id)
Run Code Online (Sandbox Code Playgroud)

换句话说,group by score然后摆弄样品编号.注意:这假设您没有重复的分数.如果这样做,您可以使用更复杂的版本:

select (case when score is null then min(sample_number) || '-' || max(sample_number)
             else min(sample_number)
        end) as sample_number,
       score
from (select t.*,
             row_number() over (partition by score order by sample_number) as seqnum
      from table t
     ) t
group by score, (case when score is not null then seqnum end);
Run Code Online (Sandbox Code Playgroud)