明智的群体数据填充

时间:2019-06-05 09:10:59

标签: sql plsql

我正在Report中工作,并且希望仅在组内不显示订单。

我尝试使用循环并将值分配给temp变量,然后进行比较

declare
l_cnt number;
cursor c1 is select * from zz_employees;
begin
dbms_output.put_line ('Order |-' || 'Material | -' || 'Salesperson |');
for i in c1
loop
  l_cnt := i.ordernumber;
  if l_cnt = i.ordernumber then 
        --dbms_output.put_line (l_cnt);
         dbms_output.put_line (l_cnt || '-' || i.materialno || '-' || i.salesperson);
  else
  dbms_output.put_line (i.ordernumber || '-' || i.materialno || '-' || i.salesperson);
  end if;
end loop;
end;

当前表数据:-

1001    9000001 James
1001    9000002 Tom
1001    9000003 Harry
1002    9000004 Voret
1002    9000005 Kzahunar
1003    9000006 Zari
1004    9000007 Neblas
1004    9000008 Anthony

预期结果

1001    9000001 James
        9000002 Tom
        9000003 Harry
1002    9000004 Voret
        9000005 Kzahunar
1003    9000006 Zari
1004    9000007 Neblas
        9000008 Anthony

1 个答案:

答案 0 :(得分:1)

无论什么情况,您在打印值时都必须稍微改变一下循环:

loop
  if l_cnt = i.ordernumber then 
    dbms_output.put_line ('    -' || i.materialno || '-' || i.salesperson);
  else
    dbms_output.put_line (i.ordernumber || '-' || i.materialno || '-' || i.salesperson);
    l_cnt := i.ordernumber;
  end if;
end loop;

或者,您可以直接在查询中省略这些值:

SELECT CASE WHEN ordernumber  = MAX(ordernumber) over (ORDER BY ROWNUM 
                                                 ROWS BETWEEN 1 preceding AND 1 preceding) 
            THEN NULL 
            ELSE ordernumber  
       END ordernumber 
     , materialno 
     , salesperson
  FROM zz_employees