0

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

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;

有任何想法吗?

4

4 回答 4

1

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

甲骨文设置

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;

输出

第一次运行查询时,您将获得:

行号 | ANNO_PROCESO | NRO_专利 | 阿里恩多斯
-----: | :----------- | ----------: | --------:
     1 | 2019 | 1 | 1

第二次运行相同的查询时,您将获得:

下一个 | ANNO_PROCESO | NRO_专利 | 阿里恩多斯
------: | :----------- | ----------: | --------:
      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 开始“序列”:

行号 | ANNO_PROCESO | NRO_专利 | 阿里恩多斯
-----: | :----------- | ----------: | --------:
     1 | 2019 | 1 | 1

db<>在这里摆弄

于 2019-12-09T15:00:26.450 回答
1

这是一个记录在案的限制

对序列值的限制

您不能在以下构造中使用 CURRVAL 和 NEXTVAL:

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
于 2019-12-09T15:00:42.820 回答
0

无法测试...

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;
于 2019-12-09T15:00:09.590 回答
0

如果您真的需要使用 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 

见小提琴

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

于 2019-12-09T15:40:12.617 回答