Perplejo al intentar agregar 2 subconsultas de recuento a la consulta principal en Oracle

programación


Estoy intentando actualizar una consulta existente para incluir 2 subconsultas, cada una de las cuales calcula un recuento diferente. La consulta principal funciona bien por sí sola y he podido hacer que las 2 subconsultas se ejecuten por sí solas; sin embargo, cuando intento unirlas, aparece un error de sintaxis “falta palabra clave”.

Aquí está mi consulta principal. Toma los datos de inspección de 3 tablas separadas y los resume por número de trabajo para mostrar cada vez que se ejecutó el programa de inspección. Los datos de inspección se dividen en diferentes tablas según el tipo de característica (agujeros, eje z o esquina). Los resultados para cada número de trabajo se pueden encontrar en solo 1 de estas 3 tablas o en varias tablas. Debido a esto, es por eso que hay una función MIN en la consulta principal, por lo que termino con una entrada por número de trabajo. Otra cosa a tener en cuenta es que cada mesa puede tener una o más funciones de sonda del mismo tipo con la diferencia de estar en una ubicación diferente.

SQL

SELECT MIN(t.timeofentry) as timeofentry, t.machine, t.tape, t.partnumber, t.spindle, t.jobnumber 
from (
     SELECT timeofentry, machine, tape, partnumber, spindle, jobnumber
      FROM PROBEVALIDATIONHOLE
      WHERE HOLENUMBER = 1 AND TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537'
      UNION
      SELECT timeofentry, machine, tape, partnumber, spindle, jobnumber
      FROM PROBEVALIDATIONZAXIS
      WHERE ZHIT = 1 AND TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537'
      UNION
      SELECT timeofentry, machine, tape, partnumber, spindle, jobnumber
      FROM PROBEVALIDATIONCORNER
      WHERE TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537'
      )t 
      GROUP BY t.machine, t.tape, t.partnumber, t.spindle, t.jobnumber
      ORDER BY timeofentry

Esto devuelve datos como:
Hora de entrada, máquina, cinta, número de pieza, husillo, número de trabajo
02/01/2024 23:13:50, NC-537, 4479, ABC, LH, 75576
02/01/2024 23:25:50, NC-537, 4479, ABC, RH, 75577

Ahora quiero agregar 2 recuentos a la consulta para mostrar tanto el número total de funciones inspeccionadas para el número de trabajo como el número total de funciones que estaban fuera de tolerancia para ese número de trabajo. Algo como esto:

Tiempo de entrada, máquina, cinta, número de pieza, husillo, número de trabajo, error, total
02/01/2024 23:13:50, NC-537, 4479, ABC, LH, 75576, 0, 4
02/01/2024 23:25:50, NC-537, 4479, ABC, RH, 75577, 2, 4

Aquí están las subconsultas para cada uno. En el que incluye las funciones fallidas, creo que necesitaré un COALESCE en algún lugar, ya que no todos los trabajos tienen funciones fallidas, por lo que la subconsulta puede devolver un valor nulo a veces.

Este incluye la cantidad de funciones fallidas:

SQL

select jobnumber, sum(fails)as TotalFails
from(
select jobnumber, count(*) as fails from probevalidationhole where TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537' and (xoutoftol = 'Yes' or youtoftol = 'Yes') group by jobnumber
union
select jobnumber, count(*) as fails from probevalidationzaxis where TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537' and Zoutoftol = 'Yes' group by jobnumber
union
select jobnumber, count(*) as fails from probevalidationcorner where TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537' and (xoutoftol = 'Yes' or youtoftol = 'Yes') group by jobnumber)
group by jobnumber

Éste incluye el número total de funciones:

SQL

select jobnumber, sum(features)as TotalFeatures
from(
select jobnumber, count(*) as features from probevalidationhole where TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537' group by jobnumber
union
select jobnumber, count(*) as features from probevalidationzaxis where TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537' group by jobnumber
union
select jobnumber, count(*) as features from probevalidationcorner where TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537' group by jobnumber)
group by jobnumber

Lo que he probado:

Esto es lo que estaba intentando cuando recibí el error de sintaxis “falta palabra clave”. Solo estaba tratando de agregar primero las funciones totales y luego hacer las funciones fallidas, pero parece que no puedo hacer que funcionen solo las funciones totales.

SQL

SELECT MIN(t.timeofentry) as timeofentry, t.machine, t.tape, t.partnumber, t.spindle, t.jobnumber, TF.totalfeatures
from (
     SELECT timeofentry, machine, tape, partnumber, spindle, jobnumber
      FROM PROBEVALIDATIONHOLE
      WHERE HOLENUMBER = 1 AND TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537'
      UNION
      SELECT timeofentry, machine, tape, partnumber, spindle, jobnumber
      FROM PROBEVALIDATIONZAXIS
      WHERE ZHIT = 1 AND TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537'
      UNION
      SELECT timeofentry, machine, tape, partnumber, spindle, jobnumber
      FROM PROBEVALIDATIONCORNER
      WHERE TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537'
      )t
      left join
      (select jobnumber, sum(features)as TotalFeatures
      from(
      select jobnumber, count(*) as features from probevalidationhole where TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537' group by jobnumber
      union
      select jobnumber, count(*) as features from probevalidationzaxis where TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537' group by jobnumber
      union
      select jobnumber, count(*) as features from probevalidationcorner where TIMEOFENTRY between '01/01/2024 12:00:00 AM' and '01/12/2024 11:59:59 PM' and Machine = 'NC-537' group by jobnumber)
      group by jobnumber) as TF
      on t.jobnumber = TF.jobnumber
      GROUP BY t.machine, t.tape, t.partnumber, t.spindle, t.jobnumber, tf.totalfeatures
      ORDER BY timeofentry

Solución 1

Me lo imaginé. Fue un estúpido error tipográfico. En la cuarta línea desde abajo tenía “como TF”, debería haber sido solo “TF”

コメント

タイトルとURLをコピーしました