Select 此处不允许使用序号

Select sequence number not allowed here

我有一个问题无法用序列解决 select,这是我的查询

SELECT  SEQ_ARRIENDO.nextval,
        TO_CHAR(SYSDATE,'YYYY') ANNO_PROCESO,
        cam.nro_patente,
        ( SELECT COUNT(ac.id_arriendo)
          FROM   arriendo_camion ac
          where  cam.nro_patente = ac.nro_patente
          and    TO_CHAR(ac.fecha_ini_arriendo,'YYYY') = TO_CHAR(SYSDATE,'YYYY') 
          having count(ac.id_arriendo) < 4
        ) "Arriendos"
FROM    camion CAM--, arriendo_camion ac
where   ( SELECT COUNT(ac.id_arriendo)
          FROM   arriendo_camion ac
          where  cam.nro_patente = ac.nro_patente 
          and    TO_CHAR(ac.fecha_ini_arriendo,'YYYY') = TO_CHAR(SYSDATE,'YYYY')
          having count(ac.id_arriendo) < 4
        ) is not null
GROUP BY cam.nro_patente,
        cam.valor_arriendo_dia,
        cam.valor_garantia_dia
order by cam.nro_patente;

有什么想法吗?

无法测试...

with t_arriendos as
(
    select 
        count(id_arriendo) count_id_arriendo,
        nro_patente nro_patente
    from arriendo_camion
    where to_char(fecha_ini_arriendo,'YYYY')= to_char(sysdate,'YYYY') 
    group by nro_patente
    having count(id_arriendo) <4
)
select
    seq_arriendo.nextval        sequence,
    to_char(sysdate,'YYYY')     anno_proceso,
    cam.nro_patente             patente,
    ac.count_id_arriendo           Arriendos
from camion cam
join t_arriendos ac
    on cam.nro_patente = ac.nro_patente
order by
    cam.nro_patente;

如果您使用序列,那么您第一次执行查询时将生成值;那么下次执行查询时,您将不会获得相同的值,但会获得序列中的下一个值。这可能不是您想要的。

Oracle 设置:

CREATE TABLE camion ( nro_patente, valor_arriendo_dia, valor_garantia_dia ) AS
SELECT 1, 1, 1 FROM DUAL;

CREATE TABLE arriendo_camion ( id_arriendo, nro_patente, fecha_ini_arriendo ) AS
SELECT 1, 1, SYSDATE FROM DUAL;

CREATE SEQUENCE SEQ_ARRIENDO;

用序列查询:

SELECT  SEQ_ARRIENDO.NEXTVAL,
        t.*
FROM    (
  SELECT  TO_CHAR(SYSDATE,'YYYY') ANNO_PROCESO,
          cam.nro_patente,
          ( SELECT COUNT(ac.id_arriendo)
            FROM   arriendo_camion ac
            where  cam.nro_patente = ac.nro_patente
            and    TO_CHAR(ac.fecha_ini_arriendo,'YYYY') = TO_CHAR(SYSDATE,'YYYY') 
            having count(ac.id_arriendo) < 4
          ) "Arriendos"
  FROM    camion CAM
  GROUP BY cam.nro_patente,
          cam.valor_arriendo_dia,
          cam.valor_garantia_dia
  order by cam.nro_patente
) t
where   "Arriendos" is not null;

输出:

你第一次运行查询你会得到:

ROWNUM | ANNO_PROCESO | NRO_PATENTE | Arriendos
-----: | :----------- | ----------: | --------:
     1 | 2019         |           1 |         1

你第二次 运行 同一个查询,你会得到:

NEXTVAL | ANNO_PROCESO | NRO_PATENTE | Arriendos
------: | :----------- | ----------: | --------:
      2 | 2019         |           1 |         1

并且序号会从上一个NEXTVAL开始递增。


查询 ROWNUM:

假设您只想要一个从 1 开始的递增整数值,然后对您的查询进行排序,然后使用 ROWNUM:

SELECT  ROWNUM,
        t.*
FROM    (
  SELECT  TO_CHAR(SYSDATE,'YYYY') ANNO_PROCESO,
          cam.nro_patente,
          ( SELECT COUNT(ac.id_arriendo)
            FROM   arriendo_camion ac
            where  cam.nro_patente = ac.nro_patente
            and    TO_CHAR(ac.fecha_ini_arriendo,'YYYY') = TO_CHAR(SYSDATE,'YYYY') 
            having count(ac.id_arriendo) < 4
          ) "Arriendos"
  FROM    camion CAM
  GROUP BY cam.nro_patente,
          cam.valor_arriendo_dia,
          cam.valor_garantia_dia
  order by cam.nro_patente
) t
where   "Arriendos" is not null;

输出:

这将始终在 1:

开始 "sequence"
ROWNUM | ANNO_PROCESO | NRO_PATENTE | Arriendos
-----: | :----------- | ----------: | --------:
     1 | 2019         |           1 |         1

db<>fiddle here

那是 documented restriction:

Restrictions on Sequence Values

You cannot use CURRVAL and NEXTVAL in the following constructs:

A subquery in a DELETE, SELECT, or UPDATE statement

A query of a view or of a materialized view

A SELECT statement with the DISTINCT operator

A SELECT statement with a GROUP BY clause or ORDER BY clause

A SELECT statement that is combined with another SELECT statement with the UNION, INTERSECT, or MINUS set operator

The WHERE clause of a SELECT statement

The condition of a CHECK constraint

如果您确实需要使用 seq,因为某些原因无法采用 MTO 答案中提供的 row_num 方式。

你应该试试这样的东西

select 
   SEQ_ARRIENDO.nextval,
   TO_CHAR(SYSDATE,'YYYY') ANNO_PROCESO,
   cam.nro_patente,
   VAC.count_arriendos
from (
   SELECT ac.nro_patente, COUNT(ac.id_arriendo) count_arriendos
   FROM   arriendo_camion ac
   where  TO_CHAR(ac.fecha_ini_arriendo,'YYYY') = TO_CHAR(SYSDATE,'YYYY')
   group by ac.nro_patente
   having count(ac.id_arriendo) < 4
) VAC
inner join camion CAM
on cam.nro_patente = VAC.nro_patente 

见fiddle

https://dbfiddle.uk/?rdbms=oracle_11.2&fiddle=3baeef28187e0a14a8b9cf04047996a0