viernes, 27 de agosto de 2010

Una consultita en Mysql...

Esta es una prueba de lo potente que puede ser Mysql.


select
tabla.DEPENDENCIA,
tabla.ETIQUETA,
tabla.DESCRIPCION,
tabla.SERIE,
tabla.OBSERVACIONES,
tabla.MARCA,
case tabla.TIPO_DATO_ATRIBUTO_MODELO
when 1 then
((select valores_catalogo.VALOR
from detalle_catalogo
inner join valores_catalogo on valores_catalogo.ID_VALOR_CATALOGO=detalle_catalogo.ID_VALOR_CATALOGO
inner join atributos on atributos.ID_ATRIBUTO=valores_catalogo.ID_ATRIBUTO
where atributos.NOMBRE='MODELO'
and detalle_catalogo.ID_ACTIVO=tabla.ID_ACTIVO))
when 2 then
((select valores_unicos.VALOR from muebles
inner join activos on activos.ID_ACTIVO=muebles.ID_ACTIVO
inner join valores_unicos on valores_unicos.ID_ACTIVO=muebles.ID_ACTIVO
inner join atributos on atributos.ID_ATRIBUTO=valores_unicos.ID_ATRIBUTO
where muebles.TIPO_mueble=1
and atributos.nombre='MODELO'
and atributos.ID_CLASIFICACION = muebles.ID_CLASIFICACION
and valores_unicos.ID_ACTIVO=tabla.ID_ACTIVO))
else ''
end as MODELO,
tabla.CURP,
tabla.ID_RESGUARDO,
tabla.UBICACION,
tabla.CLAS,
tabla.PARTIDA,
tabla.NOM_PARTIDA,
tabla.ID_ESTATUS,
tabla.ID_RESGUARDO,
if((select id_solicitud_resguardo_externo from solicitud_resguardo_externo
where id_resguardo=tabla.ID_RESGUARDO) is null,'0',(
select CONCAT(datos_resguardante_externo.AP_PATERNO,' ', datos_resguardante_externo.AP_MATERNO,' ',datos_resguardante_externo.NOMBRE)
from solicitud_resguardo_externo
inner join datos_resguardante_externo on datos_resguardante_externo.id_solicitud_resguardo_externo=solicitud_resguardo_externo.id_solicitud_resguardo_externo
where id_resguardo=tabla.ID_RESGUARDO)
) AS 'RESGUARDANTE_EXTERNO',

if((select id_solicitud_resguardo_externo from solicitud_resguardo_externo
where id_resguardo=tabla.ID_RESGUARDO) is null,'0',(
select TRIM(datos_resguardante_externo.PUESTO)
from solicitud_resguardo_externo
inner join datos_resguardante_externo on datos_resguardante_externo.id_solicitud_resguardo_externo=solicitud_resguardo_externo.id_solicitud_resguardo_externo
where id_resguardo=tabla.ID_RESGUARDO)
) AS 'PUESTO'

from (
select a.id_activo,
dependencias.NOMBRE AS 'DEPENDENCIA',
(select valores_unicos.VALOR from
valores_unicos
inner join atributos on atributos.ID_ATRIBUTO=valores_unicos.ID_ATRIBUTO
where atributos.NOMBRE='ETIQUETA'
and valores_unicos.ID_ACTIVO=a.ID_ACTIVO) as 'ETIQUETA',
muebles.DESCRIPCION,
muebles.SERIE,
muebles.OBSERVACIONES,
(select atributos.id_tipo_dato from atributos
inner join clasificaciones on clasificaciones.id_clasificacion=atributos.id_clasificacion
where clasificaciones.id_clasificacion=muebles.id_clasificacion
and atributos.NOMBRE='MODELO'
)AS 'TIPO_DATO_ATRIBUTO_MODELO',
(select valores_catalogo.VALOR
from detalle_catalogo
inner join valores_catalogo on valores_catalogo.ID_VALOR_CATALOGO=detalle_catalogo.ID_VALOR_CATALOGO
inner join atributos on atributos.ID_ATRIBUTO=valores_catalogo.ID_ATRIBUTO
where atributos.NOMBRE='MARCA'
and detalle_catalogo.ID_ACTIVO=a.ID_ACTIVO) AS 'MARCA',
if((select resguardos.curp_responsable from detalles_resguardos
inner join resguardos on resguardos.id_resguardo=detalles_resguardos.id_resguardo
where detalles_resguardos.id_estatus_resguardo in (1,2)
and detalles_resguardos.id_activo=a.id_activo) is null,'EN SOLICITUD DE BAJA',(select resguardos.curp_responsable from detalles_resguardos
inner join resguardos on resguardos.id_resguardo=detalles_resguardos.id_resguardo
where detalles_resguardos.id_estatus_resguardo in (1,2)
and detalles_resguardos.id_activo=a.id_activo)) as 'curp',
if((select resguardos.id_resguardo from detalles_resguardos
inner join resguardos on resguardos.id_resguardo=detalles_resguardos.id_resguardo
where detalles_resguardos.id_estatus_resguardo in (1,2)
and detalles_resguardos.id_activo=a.id_activo) is null,'',(select resguardos.id_resguardo from detalles_resguardos
inner join resguardos on resguardos.id_resguardo=detalles_resguardos.id_resguardo
where detalles_resguardos.id_estatus_resguardo in (1,2)
and detalles_resguardos.id_activo=a.id_activo)) AS 'ID_RESGUARDO',
oficinas.NOMBRE as 'UBICACION',
clasificaciones.NOMBRE as 'CLAS',
partidas.PARTIDA,
partidas.NOMBRE as NOM_PARTIDA,muebles.id_estatus
from activos a
inner join muebles on muebles.ID_ACTIVO=a.ID_ACTIVO
inner join oficinas on oficinas.ID_OFICINA=muebles.ID_OFICINA
inner join clasificaciones on clasificaciones.ID_CLASIFICACION = muebles.ID_CLASIFICACION
inner join partidas on partidas.ID_PARTIDA=clasificaciones.ID_PARTIDA
inner join dependencias on dependencias.ID = a.ID_DEPENDENCIA
WHERE a.TIPO_ACTIVO=1
and muebles.TIPO_MUEBLE=1
and clasificaciones.ID_PARTIDA = 10
and muebles.ID_ESTATUS <> 2
ORDER BY PARTIDA,CLAS,DESCRIPCION) tabla


Saludos...

No hay comentarios:

Publicar un comentario