El día de hoy me enfrente al detalle de obtener un reporte con multi-valores, esto es en una fila colocar los datos de una tabla y posteriormente en una sola celda aparezcan datos de otra tabla donde esta vinculada del tipo foreign key, dejo aqui la consulta para que sirva de referencia...
select
factura, fecha,
case motivoservicio
when 1 then 'Falla en Software'
when 2 then 'Garantía'
when 3 then 'Mantenimiento Preventivo'
end as servicio,
(select nombre from sucursales where CLAVE=r.clave) as sucursal,
(select list(case id_tipo
when 1 then 'Reinstalación'
when 2 then 'Virus'
when 3 then 'Configuración'
when 4 then 'Otro'
end,' / ')
from tipos_soporte a
where r.factura = a.factura group by a.factura) as soporte,
from registro_servicios r
where
cancelado = 0 and
fecha = '2012-10-02'
Esto producirá un resultado como el siguiente:
factura | fecha | servicio | sucursal | soporte |
10291 | 2012-10-02 | Falla en Software | Vallarta | Reinstalación / Virus |
10292 | 2012-10-02 | Falla en Software | Vallarta | Virus / Configuración |
10301 | 2012-10-02 | Garantía | Cihuatlán | Otro |
10306 | 2012-10-02 | Garantía | Guadalajara | Virus |
Es importante hacer mención que esta consulta será compatible con firebird versión 2.1 en delante, por el uso de LIST().
Espero les sea de utilidad.
Referencias:
http://www.janus-software.com/fbmanual/manual.php?book=psql&topic=56
http://www.developeando.com/2009/02/firebird-21-list-function.html
http://www.firebirdsql.org/refdocs/langrefupd20-select.html#langrefupd20-aggregates
http://www.firebirdsql.org/file/documentation/reference_manuals/user_manuals/html/migration-mssql-syntax.html
http://www.firebirdsql.org/manual/nullguide-aggrfunc.html
No hay comentarios:
Publicar un comentario