我有这个问题:
SELECT * FROM (
SELECT DISTINCT on (i.empleado) i.id as id,i.dia as dia,e.id as empleadoId,pe.nombre as personaNombre,pe.apellido1 as personaApellido1,pe.apellido2 personaApellido2 FROM rrhh.imputacion as i
INNER JOIN rrhh.empleado as e ON e.id = i.empleado
JOIN commons.persona AS pe ON e.persona_comun = pe.id
INNER JOIN rrhh.parte__imputaciones as pi ON pi.imputacion = i.id
INNER JOIN rrhh.parte as p ON pi.parte = p.id
WHERE p.borrador = false AND e.fecha_fin_contrato IS NULL AND e.security_domain_id = 2 and e.id
NOT IN(
SELECT DISTINCT on (i.empleado) i.empleado FROM rrhh.imputacion as i
INNER JOIN rrhh.empleado as e ON e.id = i.empleado
JOIN commons.persona AS pe ON e.persona_comun = pe.id
INNER JOIN rrhh.parte__imputaciones as pi ON pi.imputacion = i.id
INNER JOIN rrhh.parte as p ON pi.parte = p.id
WHERE i.dia >= '2017-12-04' AND i.dia <= '2017-12-10' and p.borrador = false AND e.fecha_fin_contrato IS NULL AND e.security_domain_id = 2
ORDER BY i.empleado, i.dia DESC,i.id
)
ORDER BY i.empleado, i.dia DESC,i.id)
t
ORDER BY t.dia DESC
我需要在“NOT IN”中添加以下查询,并带有OR:
SELECT DISTINCT on (a.empleado) a.empleado FROM rrhh.ausencia as a
INNER JOIN rrhh.empleado as e ON e.id = a.empleado
JOIN commons.persona AS pe ON e.persona_comun = pe.id
INNER JOIN rrhh.parte__imputaciones as pi ON pi.imputacion = a.id
INNER JOIN rrhh.parte as p ON pi.parte = p.id
WHERE a.dia >= '2017-12-04' AND a.dia <= '2017-12-11' and p.borrador = false AND e.fecha_fin_contrato IS NULL AND e.security_domain_id = 2
ORDER BY a.empleado, a.dia DESC,a.id
应该有类似于我放下的东西,但这不起作用,因为我不知道语法,我尝试了OR和UNION,但在这两种情况下我都收到错误:
SELECT * FROM (
SELECT DISTINCT on (i.empleado) i.id as id,i.dia as dia,e.id as empleadoId,pe.nombre as personaNombre,pe.apellido1 as personaApellido1,pe.apellido2 personaApellido2 FROM rrhh.imputacion as i
INNER JOIN rrhh.empleado as e ON e.id = i.empleado
JOIN commons.persona AS pe ON e.persona_comun = pe.id
INNER JOIN rrhh.parte__imputaciones as pi ON pi.imputacion = i.id
INNER JOIN rrhh.parte as p ON pi.parte = p.id
WHERE p.borrador = false AND e.fecha_fin_contrato IS NULL AND e.security_domain_id = 2 and e.id
NOT IN(
SELECT DISTINCT on (i.empleado) i.empleado FROM rrhh.imputacion as i
INNER JOIN rrhh.empleado as e ON e.id = i.empleado
JOIN commons.persona AS pe ON e.persona_comun = pe.id
INNER JOIN rrhh.parte__imputaciones as pi ON pi.imputacion = i.id
INNER JOIN rrhh.parte as p ON pi.parte = p.id
WHERE i.dia >= '2017-12-04' AND i.dia <= '2017-12-10' and p.borrador = false AND e.fecha_fin_contrato IS NULL AND e.security_domain_id = 2
ORDER BY i.empleado, i.dia DESC,i.id
OR -- UNION
SELECT DISTINCT on (a.empleado) a.empleado FROM rrhh.ausencia as a
INNER JOIN rrhh.empleado as e ON e.id = a.empleado
JOIN commons.persona AS pe ON e.persona_comun = pe.id
INNER JOIN rrhh.parte__imputaciones as pi ON pi.imputacion = a.id
INNER JOIN rrhh.parte as p ON pi.parte = p.id
WHERE a.dia >= '2017-12-04' AND a.dia <= '2017-12-11' and p.borrador = false AND e.fecha_fin_contrato IS NULL AND e.security_domain_id = 2
ORDER BY a.empleado, a.dia DESC,a.id
)
ORDER BY i.empleado, i.dia DESC,i.id)
t
ORDER BY t.dia DESC
答案 0 :(得分:0)
I need to add the query below in the "NOT IN", with an OR:
我不知道你的意思是&#34;用OR&#34;。如果您的意思是e.id
应该是NOT IN
第一个子查询或不在第二个子查询中,您可以这样做:
...
AND (
e.id NOT IN (
SELECT DISTINCT ...
) OR (
e.id NOT IN (
SELECT DISTINCT ...
)
)
如果你不希望它在任何一个(因此尝试联盟),只需将OR改为AND。
虽然我怀疑此查询的查询计划会很差。当有很多结果时,IN和NOT IN是低效的。
此外代码也很奇怪。你的大多数联接都是明确的INNER JOIN,但有几个只是说JOIN。但是默认情况下JOIN是INNER JOIN。那么为什么有些线条明确而其他线条隐含?为什么整个代码的样式不同?例如,AS
vs as
,从一行到下一行...