我实际上是SQL的初学者,我在使用Oracle引擎。我有一个问题,使用Interval进行算术操作,添加timestamp列 - 整数值,存在于其他表中并将其转换为Minute。 测试我在数据生成器中使用的模式。结果,产生的一些数据不可靠,当需要两次治疗的同一患者重叠时,我需要检查两次预约之间的重叠。 我有的treatment_appointments表包含这些属性:
treatments_appointments(app_id NUMBER(38) NOT NULL,
[fk] care_id NUMBER(38) NOT NULL,
[fk] doctor_id NUMBER(38) NOT NULL,
[fk] room_id NUMBER(38) NOT NULL,
[fk] branch_id NUMBER(38) NOT NULL,
[fk] patient_id NUMBER(38) NOT NULL,
appointment_time TIMESTAMP NOT NULL)
以下是我编写的代码,它会收到一条错误消息:
SELECT app1.app_id
FROM treatment_appointment app1
INNER JOIN treatment_appointment app2
ON app1.patient_id = app2.patient_id
WHERE app1.appointment_time >= app2.appointment_time AND
app1.appointment_time <=
app2.appointment_time + interval (to_char(select care_categories.care_duration where app2.care_id = care_categories.care_id)) minute
AND
app1.app_id != app2.app_id
错误消息是: ORA-00936:缺少表达 对不起我的英语,感谢您回答我的问题!
答案 0 :(得分:1)
您只能为an INTERVAL
literal使用固定的字符串值,而不能使用变量,表达式或列值。但您可以使用NUMTODSINTERVAL
function将分钟数转换为间隔。而不是:
interval (to_char(select care_categories.care_duration
where app2.care_id = care_categories.care_id)) minute
使用:
numtodsinterval(select care_categories.care_duration
where app2.care_id = care_categories.care_id, 'MINUTE')
虽然您应该在主查询中加入该表,而不是为每一行执行子查询:
SELECT app1.app_id
FROM treatment_appointment app1
INNER JOIN treatment_appointment app2
ON app1.patient_id = app2.patient_id
INNER JOIN care_categories cc
ON app2.care_id = cc.care_id
WHERE app1.appointment_time >= app2.appointment_time AND
app1.appointment_time <=
app2.appointment_time + numtodsinterval(cc.care_duration, 'MINUTE') AND
app1.app_id != app2.app_id