Mostrando entradas con la etiqueta database. Mostrar todas las entradas
Mostrando entradas con la etiqueta database. Mostrar todas las entradas

lunes, 10 de marzo de 2014

DB2 - Consultas de metadatos - Consultas recursivas



DB2 – consulta de metadatos

Catalog Schema: Doc original / standard name: syscat à DB2 alias: ibm

triggers
select text from sysibm.sysTRIGGERS where tbname

secuencias
select * from sysibm.syssequences where owner

stored procedure / function
select * from SYSIBM.sysroutines where owner

PK, unique constraint, FK,
select * from SYSIBM.syskeycoluse where constname
select * from SYSIBM.SYSRELS where RELNAME
select * from SYSIBM.SYSFOREIGNKEYS where relname

tables
FROM SYSIBM.SYSTABLES 
FROM SYSIBM.SYSCOLUMNS where tbname and tbcreator

Dummy
sysibm.sysdummy1

Consultas recursivas en DB2 (jerarquía jerárquicas hierarchy hierarchial recursive)

/************************************************************************/
/************************************************************************/
/******************* COMPARE THE FOLLOWING 2 QUERIES  *******************/
/************************************************************************/
/************************************************************************/
-- TOP-DOWN
with MY_HIERARCHY (MY_NODE, MY_PARENTNODE) as (
      -- non-recursive exit
      select ROOT.MY_NODE, ROOT.MY_PARENTNODE
      from THETABLE ROOT where MY_NODE='value'   -- the root node in the tree

      union all
      -- recursive loop (it needs the non-recursive part in order not to be an empty set)
      select CHILD.MY_NODE, CHILD.MY_PARENTNODE
      from MY_HIERARCHY PARENT, THETABLE CHILD
      where CHILD.MY_PARENTNODE = PARENT.MY_NODE   -- top-down join

)
select * from MY_HIERARCHY


-- BOTTOM-UP
with MY_HIERARCHY (MY_NODE, MY_PARENTNODE) as (
      -- non-recursive exit
      select LEAF.MY_NODE, LEAF.MY_PARENTNODE
      from THETABLE LEAF where MY_NODE='value'   -- the leaf node in the tree

      union all
      -- recursive loop (it needs the non-recursive part in order not to be an empty set)
      select CHILD.MY_NODE, CHILD.MY_PARENTNODE
      from MY_HIERARCHY PARENT, THETABLE CHILD
      where CHILD.MY_NODE = PARENT.MY_PARENTNODE   -- bottom-up join

)
select * from MY_HIERARCHY

lunes, 16 de diciembre de 2013

SQL - selección en base a rangos de fechas

Patrón:
- La relación tiene atributos de fecha inicio y fin (fecha_inicio_relación y fecha_fin_relación).
- La selección se quiere hacer según un rango fecha inicio y fin (fecha_inicio_selección y fecha_fin_selección), seleccionando todas en las que los segmentos se toquen o se superpongan (ver imagen):




fecha_inicio_relación                         fecha_fin_relación
|——————————————|
fecha_inicio_selección                       fecha_fin_selección
|——————————————|


Query:

select * from relación where (fecha_inicio_relación <= fecha_inicio_selección and fecha_fin_relación >= fecha_inicio_selección or fecha_inicio_relación <= fecha_fin_selección and fecha_fin_relación >= fecha_fin_selección or fecha_inicio_relación >= fecha_inicio_selección and fecha_fin_relación <= fecha_fin_selección or fecha_inicio_relación <= fecha_inicio_selección and fecha_fin_relación >= fecha_fin_selección)
-- validaciones de consistencia
and fecha_inicio_relación <= fecha_fin_relación and fecha_inicio_selección <= fecha_fin_selección

martes, 3 de septiembre de 2013

DB2 - manejar jerarquías de idiomas

En DB2, seleccionar datos de una tabla "tabla_origen", buscando con una "mi_clave_busqueda" que tiene multiplicidad n (los datos están en n idiomas), de los cuales me interesa recuperar el idioma que (para mí) tenga mayor prioridad, si no el siguiente, y así sucesivamente.



with mis_idiomas as(
      select * from table (
            select 'es',  1 from sysibm.sysdummy1 union all
            select 'fr',  2 from sysibm.sysdummy1 union all
            select 'en',  3 from sysibm.sysdummy1 union all
            select 'he',  4 from sysibm.sysdummy1
      ) as t (idioma, ord)
),
T2 as(
      select tor.mi_clave_busqueda, min(ord) as ord from tabla_origen tor inner join mis_idiomas ml on tor.cod_idioma=ml.idioma group by tor.mi_clave_busqueda
)
select tor.mis_valores_buscados from T2 inner join mis_idiomas ml on T2.ord=ml.ord inner join tabla_origen tor on T2.mi_clave_busqueda=tor.mi_clave_busqueda and ml.idioma=tor.idioma