oracle SQL额外的空行

6xfqseft  于 2023-10-16  发布在  Oracle
关注(0)|答案(2)|浏览(137)

我需要我的SQL查询返回15(十五)行。如果它返回少于15行,它需要用空行来完成,如果它返回超过15行,它只显示前15行。有人能帮帮我吗

select * from (select IDCODFATURA,  
       PAGINA, 
       TARIFA_RES_TE, 
       VALOR_ICMS_TE, 
       TARIFA_COM_ICMS_TE, 
       TARIFA_SEM_ICMS_TE, 
       VALOR_PISCOFINS_TE, 
       VALOR_COM_ICMS_TE, 
       VALOR_SEM_ICMS_TE, 
       VALOR_BASE_ICMS,
       TARIFA_TUSD,
       case 
            when nvl(param_com_tributos.tipo_imp_imposto_fat,0) = 0 then base_fatura_itens.valor_com_icms
                else base_fatura_itens.VALOR_SEM_PCICMS
         end as valor, 
       case 
            when nvl(param_com_tributos.tipo_imp_imposto_fat,0) = 0 then base_fatura_itens.tarifa_com_icms
                else base_fatura_itens.tarifa_res
         end as tarifa,
       TARIFA_TE,
       usuario,
       GUID,
       TIPO_DESCONTO_MICROGERACAO,
       (select VALOR_PIS 
          from base_fatura_web 
         where IDCODFATURA = :IDCODFATURA
           and PAGINA = :PAGINA
           and GUID = :GUID
           and usuario = :usuario) as VALOR_PIS,
       (select VALOR_COFINS 
          from base_fatura_web 
         where IDCODFATURA = :IDCODFATURA
           and PAGINA = :PAGINA
           and GUID = :GUID
           and usuario = :usuario) as VALOR_COFINS
      from base_fatura_itens,
           param_com_tributos
     where IDCODFATURA = :IDCODFATURA
       and PAGINA = :PAGINA
        and param_com_tributos.idparam=1 
        and base_fatura_itens.GUID = :GUID
        and base_fatura_itens.usuario = :usuario
        and base_fatura_itens.descricao <> 'ICMS Descontos tarifários'
      order by classificacao,sequencia)
      where rownum <=15
pvabu6sv

pvabu6sv1#

正如注解中提到的,使用一个总是提供15行的帮助表。下面的例子是关于emp/dept样本数据的,但是你也应该能够对你的数据集这样做。emp表有14行。
步骤1:生成15个空行

select level
  from dual
connect by
   level - 1< 15

步骤2:向原始查询添加行号。这将用于连接到帮助器表。把顺序改为你需要满足的“前15名”条件。

select ROW_NUMBER() OVER (PARTITION BY 1 ORDER BY e.ename),  
       e.ename, 
       e.job 
  from emp e

现在把它们放在一起:

with dummy_rows (rn) as
(
select level
  from dual
connect by
    level - 1< 15
)
,
my_emp_query(rn, ename, job) as
(
select ROW_NUMBER() OVER (PARTITION BY 1 ORDER BY e.ename),  
       e.ename, 
       e.job 
  from emp e 
)
select d.rn, e.ename, e.job 
  from 
  dummy_rows d
  left outer join my_emp_query e on d.rn = e.rn    ;

        RN ENAME      JOB      
---------- ---------- ---------
         1 ADAMS      CLERK    
         2 ALLEN      SALESMAN 
         3 BLAKE      MANAGER  
         4 CLARK      MANAGER  
         5 FORD       ANALYST  
         6 JAMES      CLERK    
         7 JONES      MANAGER  
         8 KING       PRESIDENT
         9 MARTIN     SALESMAN 
        10 MILLER     CLERK    
        11 SCOTT      ANALYST  
        12 SMITH      CLERK    
        13 TURNER     SALESMAN 
        14 WARD       SALESMAN 
        15                     

15 rows selected.

注意第15行是空的,因为emp表中没有匹配的记录。把15改成5,你只会得到5行。

m0rkklqb

m0rkklqb2#

我做到了!可能Koen Lostrie的答案比我做的更好,但我得到了我公司某个人的帮助,我认为它最符合我们的标准。代码如下:

SELECT *
    FROM (SELECT IDCODFATURA,
                 PAGINA,
                 TARIFA_RES_TE,
                 VALOR_ICMS_TE,
                 TARIFA_COM_ICMS_TE,
                 TARIFA_SEM_ICMS_TE,
                 VALOR_PISCOFINS_TE,
                 VALOR_COM_ICMS_TE,
                 VALOR_SEM_ICMS_TE,
                 VALOR_BASE_ICMS,
                 TARIFA_TUSD,
                 CASE
                     WHEN NVL (param_com_tributos.tipo_imp_imposto_fat, 0) = 0
                     THEN
                         base_fatura_itens.valor_com_icms
                     ELSE
                         base_fatura_itens.VALOR_SEM_PCICMS
                 END                                AS valor,
                 CASE
                     WHEN NVL (param_com_tributos.tipo_imp_imposto_fat, 0) = 0
                     THEN
                         base_fatura_itens.tarifa_com_icms
                     ELSE
                         base_fatura_itens.tarifa_res
                 END                                AS tarifa,
                 TARIFA_TE,
                 usuario,
                 GUID,
                 TIPO_DESCONTO_MICROGERACAO,
                 (SELECT VALOR_PIS
                    FROM base_fatura_web
                   WHERE     IDCODFATURA = :IDCODFATURA
                         AND PAGINA = :PAGINA
                         AND GUID = :GUID
                         AND usuario = :usuario)    AS VALOR_PIS,
                 (SELECT VALOR_COFINS
                    FROM base_fatura_web
                   WHERE     IDCODFATURA = :IDCODFATURA
                         AND PAGINA = :PAGINA
                         AND GUID = :GUID
                         AND usuario = :usuario)    AS VALOR_COFINS
            FROM base_fatura_itens, param_com_tributos
           WHERE     IDCODFATURA = :IDCODFATURA
                 AND PAGINA = :PAGINA
                 AND param_com_tributos.idparam = 1
                 AND base_fatura_itens.GUID = :GUID
                 AND base_fatura_itens.usuario = :usuario
                 AND base_fatura_itens.descricao <>
                     'ICMS Descontos tarifários'
                 AND ROWNUM <= 15
          UNION ALL
              SELECT NULL     AS IDCODFATURA,
                     NULL     AS PAGINA,
                     NULL     AS TARIFA_RES_TE,
                     NULL     AS VALOR_ICMS_TE,
                     NULL     AS TARIFA_COM_ICMS_TE,
                     NULL     AS TARIFA_SEM_ICMS_TE,
                     NULL     AS VALOR_PISCOFINS_TE,
                     NULL     AS VALOR_COM_ICMS_TE,
                     NULL     AS VALOR_SEM_ICMS_TE,
                     NULL     AS VALOR_BASE_ICMS,
                     NULL     AS TARIFA_TUSD,
                     NULL     AS valor,
                     NULL     AS tarifa,
                     NULL     AS TARIFA_TE,
                     NULL     AS usuario,
                     NULL     AS GUID,
                     NULL     AS TIPO_DESCONTO_MICROGERACAO,
                     NULL     AS VALOR_PIS,
                     NULL     AS VALOR_COFINS
                FROM DUAL
          CONNECT BY LEVEL <=
                     (  15
                      - (SELECT COUNT (*)
                           FROM base_fatura_itens, param_com_tributos
                          WHERE     IDCODFATURA = :IDCODFATURA
                                AND PAGINA = :PAGINA
                                AND param_com_tributos.idparam = 1
                                AND base_fatura_itens.GUID = :GUID
                                AND base_fatura_itens.usuario = :usuario
                                AND base_fatura_itens.descricao <>
                                    'ICMS Descontos tarifários')))
   WHERE ROWNUM <= 15
ORDER BY classificacao, sequencia

相关问题