我有一个问题,就是我无法按顺序解决选择问题,这是我的查询
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;
有什么想法吗?
答案 0 :(得分:1)
如果使用序列,则第一次执行查询时将生成值;那么下一次执行查询时,您将不会获得相同的值,但会获得序列中的下一个值。这可能不是您想要的。
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开始“序列”。
ROWNUM | ANNO_PROCESO | NRO_PATENTE | Arriendos -----: | :----------- | ----------: | --------: 1 | 2019 | 1 | 1
db <>提琴here
答案 1 :(得分: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
答案 2 :(得分: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;
答案 3 :(得分: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