-- DROP VIEW liv_tiempo_trabajador; CREATE OR REPLACE VIEW liv_tiempo_trabajador AS SELECT 'Activas' as tipo, a.empresa, a.trabajador, (d.nombre::text || ' '::text) || d.apellidos::text AS nombre_trabajador, a.id_accion, h.nombre AS nombre_accion, a.fecha_hora_inicio, a.fecha_hora_fin, a.parte, a.fecha, a.id_linea, a.n_linea, a.n_accion, c.terminado, date(a.fecha_hora_inicio) AS fecha_inicio, to_char(a.fecha_hora_inicio, 'HH24:MI:SS'::text) AS hora_inicio, date(a.fecha_hora_fin) AS fecha_final, to_char(a.fecha_hora_fin, 'HH24:MI:SS'::text) AS hora_final, div(date_part('epoch'::text, a.fecha_hora_fin - a.fecha_hora_inicio)::numeric, 60::numeric) AS minutos, date_part('epoch'::text, a.fecha_hora_fin - a.fecha_hora_inicio) AS segundos, (to_char(div(date_part('epoch'::text, a.fecha_hora_fin - a.fecha_hora_inicio)::numeric, 3600::numeric), 'FM99900'::text) || ':'::text) || to_char(div(mod(date_part('epoch'::text, a.fecha_hora_fin - a.fecha_hora_inicio)::numeric, 3600::numeric), 60::numeric), 'FM00'::text) AS tiempo_total, ((to_char(div(date_part('epoch'::text, a.fecha_hora_fin - a.fecha_hora_inicio)::numeric, 3600::numeric), 'FM99900'::text) || ':'::text) || to_char(div(mod(date_part('epoch'::text, a.fecha_hora_fin - a.fecha_hora_inicio)::numeric, 3600::numeric), 60::numeric), 'FM00'::text || ':'::text)) || to_char(mod(mod(date_part('epoch'::text, a.fecha_hora_fin - a.fecha_hora_inicio)::numeric, 3600::numeric), 60::numeric), 'FM00'::text) AS tiempo_total_s, CASE WHEN c.estado IS NULL THEN 'Fuera de Linea'::text WHEN c.estado = 'A'::bpchar THEN 'Linea produciendo'::text WHEN c.estado = 'R'::bpchar THEN 'Linea produciciendo con problemas'::text WHEN c.estado = 'P'::bpchar THEN 'Linea parada'::text ELSE NULL::text END AS nombre_estado, b.articulo, b.formato FROM li30itra a JOIN li10acci h ON a.empresa = h.empresa AND a.id_accion = h.id_accion LEFT JOIN li22apar b ON a.empresa = b.empresa AND a.fecha = b.fecha AND a.parte = b.parte AND a.id_linea = b.id_linea AND a.n_linea = b.n_linea LEFT JOIN li23ipar c ON a.empresa = c.empresa AND a.fecha = c.fecha AND a.parte = c.parte AND a.id_linea = c.id_linea AND a.n_linea = c.n_linea AND a.n_accion = c.n_accion LEFT JOIN li03trab d ON a.empresa = d.empresa AND a.trabajador = d.trabajador UNION ALL SELECT 'Historico' as tipo, a.empresa, a.trabajador, (d.nombre::text || ' '::text) || d.apellidos::text AS nombre_trabajador, a.id_accion, h.nombre AS nombre_accion, a.fecha_hora_inicio, a.fecha_hora_fin, a.parte, a.fecha, a.id_linea, a.n_linea, a.n_accion, c.terminado, date(a.fecha_hora_inicio) AS fecha_inicio, to_char(a.fecha_hora_inicio, 'HH24:MI:SS'::text) AS hora_inicio, date(a.fecha_hora_fin) AS fecha_final, to_char(a.fecha_hora_fin, 'HH24:MI:SS'::text) AS hora_final, div(date_part('epoch'::text, a.fecha_hora_fin - a.fecha_hora_inicio)::numeric, 60::numeric) AS minutos, date_part('epoch'::text, a.fecha_hora_fin - a.fecha_hora_inicio) AS segundos, (to_char(div(date_part('epoch'::text, a.fecha_hora_fin - a.fecha_hora_inicio)::numeric, 3600::numeric), 'FM99900'::text) || ':'::text) || to_char(div(mod(date_part('epoch'::text, a.fecha_hora_fin - a.fecha_hora_inicio)::numeric, 3600::numeric), 60::numeric), 'FM00'::text) AS tiempo_total, ((to_char(div(date_part('epoch'::text, a.fecha_hora_fin - a.fecha_hora_inicio)::numeric, 3600::numeric), 'FM99900'::text) || ':'::text) || to_char(div(mod(date_part('epoch'::text, a.fecha_hora_fin - a.fecha_hora_inicio)::numeric, 3600::numeric), 60::numeric), 'FM00'::text || ':'::text)) || to_char(mod(mod(date_part('epoch'::text, a.fecha_hora_fin - a.fecha_hora_inicio)::numeric, 3600::numeric), 60::numeric), 'FM00'::text) AS tiempo_total_s, CASE WHEN c.estado IS NULL THEN 'Fuera de Linea'::text WHEN c.estado = 'A'::bpchar THEN 'Linea produciendo'::text WHEN c.estado = 'R'::bpchar THEN 'Linea produciciendo con problemas'::text WHEN c.estado = 'P'::bpchar THEN 'Linea parada'::text ELSE NULL::text END AS nombre_estado, b.articulo, b.formato FROM li40itra a JOIN li10acci h ON a.empresa = h.empresa AND a.id_accion = h.id_accion LEFT JOIN li92apar b ON a.empresa = b.empresa AND a.fecha = b.fecha AND a.parte = b.parte AND a.id_linea = b.id_linea AND a.n_linea = b.n_linea LEFT JOIN li93ipar c ON a.empresa = c.empresa AND a.fecha = c.fecha AND a.parte = c.parte AND a.id_linea = c.id_linea AND a.n_linea = c.n_linea AND a.n_accion = c.n_accion LEFT JOIN li03trab d ON a.empresa = d.empresa AND a.trabajador = d.trabajador;