按子句分组-仅第一个“分组行”中填充了值

时间:2019-03-03 17:04:33

标签: sql oracle group-by

嗨,我需要你的帮助!

实际上我有一条select语句,其中包含一个由col1,col2,col3组成的组

enter image description here

但是我需要的是,仅“组”的第一行应显示该值。以下各行应为null或空白。它应该看起来像这样:

enter image description here

有可能吗??如果是的话,如何? :-)

感谢您的帮助!! 安雅

2 个答案:

答案 0 :(得分:0)

通常称为 break 。 SQL * Plus拥有它,Oracle Reports也有它,Application Express也有它...因此,根据您打算在哪里使用它,有可能通过选择要使用的列来声明性地执行它。重新破坏报告。

这是一个SQL * Plus示例。

首先,斯科特的EMP表“保持不变”:

SQL> select deptno, job, ename, sal
  2  from emp
  3  order by deptno, job, ename;

    DEPTNO JOB       ENAME             SAL
---------- --------- ---------- ----------
        10 CLERK     MILLER           1300
        10 MANAGER   CLARK            2450
        10 PRESIDENT KING             5000
        20 ANALYST   FORD             3000
        20 ANALYST   SCOTT            3000
        20 CLERK     ADAMS            1100
        20 CLERK     SMITH             800
        20 MANAGER   JONES            2975
        30 CLERK     JAMES             950
        30 MANAGER   BLAKE            2850
        30 SALESMAN  ALLEN            1600
        30 SALESMAN  MARTIN           1250
        30 SALESMAN  TURNER           1500
        30 SALESMAN  WARD             1250

14 rows selected.

如果您申请休息,那么它看起来像这样:

SQL> break on deptno on job
SQL> select deptno, job, ename, sal
  2  from emp
  3  order by deptno, job, ename;

    DEPTNO JOB       ENAME             SAL
---------- --------- ---------- ----------
        10 CLERK     MILLER           1300
           MANAGER   CLARK            2450
           PRESIDENT KING             5000
        20 ANALYST   FORD             3000
                     SCOTT            3000
           CLERK     ADAMS            1100
                     SMITH             800
           MANAGER   JONES            2975
        30 CLERK     JAMES             950
           MANAGER   BLAKE            2850
           SALESMAN  ALLEN            1600
                     MARTIN           1250
                     TURNER           1500
                     WARD             1250

14 rows selected.

几乎是您想要的。

答案 1 :(得分:0)

您可以将first_value函数与between 3 following and unbounded following窗口子句一起使用:

with t(col1,col2,col3,col4) as
(
 select 'A', 'A1', .3, 'Pramisse 1' from dual union all  
 select 'A', 'A1', .3, 'Pramisse 2' from dual union all    
 select 'A', 'A1', .3, 'Pramisse 3' from dual union all 
 select 'A', 'A1', .3, 'Pramisse 4' from dual union all     
 select 'A', 'A2',  1, 'Pramisse 1' from dual union all    
 select 'A', 'A2',  1, 'Pramisse 2' from dual union all 
 select 'A', 'A2',  1, 'Pramisse 3' from dual union all 
 select 'A', 'A2',  1, 'Pramisse 4' from dual union all     
 select 'A', 'A3',  1, 'Pramisse 1' from dual union all    
 select 'A', 'A3',  1, 'Pramisse 2' from dual union all 
 select 'A', 'A3',  1, 'Pramisse 3' from dual union all 
 select 'A', 'A3',  1, 'Pramisse 4' from dual   
)    
select first_value(col1) over 
         ( partition by col2 order by col4 rows 
           between 3 following and unbounded following ) as col1, 
       first_value(col2) over 
         ( partition by col2 order by col2 rows 
           between 3 following and unbounded following ) as col2, 
       first_value(col3) over 
         ( partition by col2 order by col4 rows 
           between 3 following and unbounded following ) as col3, 
       col4
  from t;

COL1    COL2    COL3    COL4
----    ----    -----   ----------
A       A1      0,30    Pramisse 1
                        Pramisse 2
                        Pramisse 3
                        Pramisse 4
A       A2      1       Pramisse 1
                        Pramisse 2
                        Pramisse 3
                        Pramisse 4
A       A3      1       Pramisse 1
                        Pramisse 2
                        Pramisse 3
                        Pramisse 4

Demo